Setup Cron Jobs in Linux


Cron jobs will automatically run during certain periods, specified by the user. With a cron job, you can schedule your backup script to automatically run at certain intervals. The output of each cron job will get e-mailed to the user.

Cron Syntax


*     *     *     *     *  command
-     -     -     -     -
|     |     |     |     |
|     |     |     |     +----- day of week (0 - 6) (Sunday=0)
|     |     |     +------- month (1 - 12)
|     |     +--------- day of month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)
A * means all legal values for that column. The value column can also have a list of elements separated by commas. A range can be given using a hyphen.

Creating/Editing Cron Jobs


A normal user can edit their cron jobs by typing:
$ crontab -e
You can also list or remove your crontab with the -l and -r switches.

The root user can also edit the /etc/crontab file. This adds the option to specify the user by which the cron job is run. The syntax is as follows:
m h dom mon dow user command

Cron Examples


30 19 * * * /usr/local/bin/runbackups
Run your backup script every day at 7:30 PM.
30 19 * * * /usr/local/bin/runbackups >> /dev/null 2>&1
Run your backup script every day at 7:30 PM, but don't e-mail the results. More specifically, The >> /dev/null 2>&1 part means to send any standard output to /dev/null (the linux trash can) and to redirect standard error (2) to the same place as the standard output (1).
5,10 0 * * 1 /var/www/checkusers.sh
Run your checkusers script at 12:05 and 12:10 every Monday.
2 0 */5 * * /file.pl
Run file.pl at 2:00 AM every 5 days.

Notes


You can change the text editor the crontab command uses by typing at the terminal:
$ export EDITOR=vi

The administrator can view another user's crontab by typing:
crontab -u USER -l
These crontabs are stored in /var/spool/cron/crontabs/