This website is not maintained anymore. Please visit www.nerdgirl.dkinstead  
This page was last modified: July 27 2010 13:22:13   
Too Cool for Internet Explorer

Crontab - how to make cronjobs

It's easy to make your server carry out tasks by itself at a specific time. This is done with crontab (1).

Command options

The crontab file is located in /etc/crontab. But never edit the crontab file directly - always use the contab command. It is always a bad idea to change system files and /etc/crontab is a system file. It could therefore in theory, change during a future update of FreeBSD.

crontab -l List the content of crontab
crontab -e Edit the crontab file
crontab -r Remove the crontab file (be carefull with that)

The u- (user) option can be used in conjuction with all of the above to list/edit/remove crontab for a specific user. This option is not necessary if you are logged in as that user.

Adding a job to cron

The format of crontab is ...

minute hour day month weekday who command

Minute - Minutes after the hour (0-59).
Hour - 24-hour format (0-23)
Day - Day of the month (1-31)
Month - Month of the year (1-12)
Weekday - Day of the week (0-6), the 0 refers to Sunday
Who - (optional) Who this job should run as
Command - The command to execute at the specified time

I have a perlscript that fetches news from a newsgroup, and saves them in a database. I want this script to run every 10 minutes.

First I tell crontab that I want to edit the crontab file:

crontab -e

crontab now opens the crontab file with my default editor. This line is then inserted:

0,10,20,30,40,50 * * * * /path/to/script/scriptname.pl

When you save and exit, the system wil automatically update with the newly added job(s)

The above relates to each field in crontab like this:

Minute 0,10,20,30,40,50
Hour *
Day *
Month *
Weekday *
Command /path/to/script/scriptname.pl

Now, imagine what a cronjob running every 5 minutes would look like... not a pretty sight... but fortunately there is at shortcut:

0-59/5 * * * * /path/to/script/scriptname.pl

Here is a few other examples:

30 0 * * * /path/to/scriptname.pl
0,10,50 9-15 * * * /path/to/scriptname.pl
0-59/10 * * * 1,3,5 /path/to/scripname.pl

30 0 * * * /path/to/scriptname.pl
Run the script thirty minutes (30) past midnight (0), on every day of the month (*), and every day of the year (*), and every day of the week (*).

0,10,50 9-15 * * * /path/to/scriptname.pl
Run the script every 0 minutes, 10 minutes, and 50 minutes past the hours (0,10,50), between 9am and 5pm (9-15), every day of the year.

0-59/10 * * * 1,3,5 /path/to/scripname.pl
Run the script every 10 minutes (0-59/10), every Monday, Wednesday, and Friday (1,3,5).

Each crontab is related to a user. If you are logged in as root when editing crontab, the jobs you add will be run as root. If you have a job that should be run as user joe, you must su to that user or use the -u option when adding the job with crontab -e

As mentioned ealier, the system crontab is located in /etc/. Other users crontab files, is located in /var/cron/tabs/

Directing the output...

Cron will by default mail any output to the owner of the job. But it is also possible to redirect output to a logfile, or maybe you do just want to trash it...

Use >/dev/null 2>&1, if you don't want any output anywhere:

0-59/10 * * * * /path/to/script/scriptname.pl>/dev/null 2>&1

In this example, errors are logged to a file and anything else is thrown overboard:

0-59/10 * * * * /path/to/script/scriptname.pl
>/dev/null 2>>/path/to/error_log

We can also log errors to one file, and anything else to another file:

0-59/10 * * * * /path/to/script/scriptname.pl
>>/path/to/log 2>>/path/to/error_log

Remember to write it all in one line when editing crontab.

.. or we can mail it anywhere we want...:
The below example is actually written in two lines...

MAILTO="name@example.com"
0-59/10 * * * * /path/to/script/scriptname.pl

When specifying MAILTO in the crontab file, output from all the jobs in that file will be sent to that mail address, unless dev/null, a log file, or another mail address is specified. Trashing or directing output to a file is already described, and here is how to direct a specific job to an email address:

0-59/10 * * * * /path/to/script/scriptname.pl | mail -s "Subject of mail" user@domain.tld

... and remember to put it all on one line.

For further informations on this subject, see crontab(1) and cron(8) of the man pages.