FreeBSD Server Administration

April 20, 2006

Portsnap

Portsnap is another tool for updating Ports tree, compared to CVSup, it's more secure, faster and easier to use. You should give it a shot, especially when you build a new server.

Update ports tree
# portsnap fetch update

For complete guide, check out the handbook "Using Portsnap".

Posted by FreeBSD Newbie at 04:03 PM | Comments (0)

August 27, 2005

Exclude a Sub-directory in Tar Command

To backup the directory /home/ to file /backups/home.tgz, and exclude /home/apachelog/ :

# cd /home
# tar czvf /backups/home.tgz --exclude apachelog/ *

Posted by FreeBSD Newbie at 01:14 AM | Comments (0)

July 30, 2005

Keep Accurate Time with ntpd and ntpdate

Obviously, keeping the server time accurate is very important. I should have done this before the server was public.

Adjust server time manually
# ntpdate pool.ntp.org

Alternatively, you can make the process automate
1. Create a file /etc/ntp.conf with the following content:
server pool.ntp.org
driftfile /etc/ntp.drift

2. Start ntpd:
# ntpd

3. To enable ntpd to start at boot time, add one line in /etc/rc.conf: (optional)
ntp_enable="YES"

The ntp server "pool.ntp.org" is recommended by ntp.org, you can also select another one, check out ntp.org for details.

Posted by FreeBSD Newbie at 05:47 PM | Comments (2)

April 12, 2005

Tar Command

Tar can detect an archive with gz compression and extract the files properly, I used to call gunzip first :-(.

Example
# tar czvf backup.tar.gz *

Extract the archive:
# tar xvf backup.tar.gz

How I did before:
# gunzip dv backup.tar.gz
# tar xvf backup.tar

Posted by FreeBSD Newbie at 11:56 PM | Comments (0)

November 06, 2004

Screen Command

My Windows XP suddenly crashed when I was running a long-time Linux script, then I tried to find a way to regain the lost output, but it seems not possible, during the search on Google, I found a very useful Linux command: Screen

You can open a virtual terminal with screen command, in which you can do everything like in the normal terminal, but you can detach it if you want to do other things in the main terminal and reattach it later. It even works after you quit your main terminal, for example, your Windows crashs :-).

Common screen command:
1. Start a new terminal:
# screen
2. Type CTRL+A and D to detach this new terminal
3. Reattach it:
# screen -R
If you have more than one screens, you will be reminded how to specify certain screen, it has very clear on-screen instruction.
List all screens:
# screen -list
Reattach a screen by pid:
# screen -R pid

Detailed usage:

Continue reading "Screen Command"

Posted by FreeBSD Newbie at 05:27 AM | Comments (1)

November 03, 2004

Cron Job

Cron job is a very convenient tool in Linux, there are lots of information if you search "cron job" on Google, for example, this is a very comprehensive guide.

It seems to be easy, but it took me a lot of time to setup a cron job, here are some advice if you can't make your cron job running.

1. Add a mailto line at the beginning, it will send you the result by email, it's very helpful to debug the problems.
MAILTO="yourname@yourdomain.com"

2. Cron job has different environment from your shell command. Even you can run your script at the command line, it doesn't mean cron job has no problem with it. You can get cron job environment using the following code, then compare with the command line result by running "env", make sure you have inserted the mailto line.
* * * * * /usr/bin/env

3. Always use absolute path. Cron job doesn't run a script in the directory as you expected. Besides the script path, if the script itself takes file names as parameter, you also must use absolute path, my cron job failed here, for example:
30 * * * * /home/username/run.sh

Content of run.sh:
/usr/bin/php /home/username/www/dosomething.php

You should be able to solve most cron job problems with above tips.

Open question for myself:
1. How to modify cron job environment?
2. MAILTO="account_name" (not full email address) doesn't work. Email to other accounts at the command line doesn't work either, this should be related with the server email configuration.
3. It seems that the MAILTO sends all cron jobs result, I don't want to watch every task, is it possible to specify only certain cron jobs?

Eric answered my question, it works great, thanks!
----------------------------
You can redirect the output of a cron job to null, then it won't be mailed to you. Where you currently have the command to execute, such as:
/path/to/somecommand
change it to be like this:
/path/to/somecommand >/dev/null

Since you can redirect to null on individual jobs, you can choose those jobs which you no longer want email from.
----------------------------

Continue reading "Cron Job"

Posted by FreeBSD Newbie at 02:05 PM | Comments (2)