Crontab – How it is used

  Linux

Crontab is a great utility to automate scripts and processes on your Linux/Unix/Mac machine. Regular users and root each have access to this utility.

To check the cron jobs a particular user already has you can run the following from the command line.

crontab -l

This will list all current running cron jobs.

Example:

[root@web ~]# crontab -l
  0 0,4,8,12,16,20 * * * rdate -s clock.xmission.com > /dev/nul 2>&1
  50 11,17,23 * * * /var/www/cgi-bin/awstats.pl -config=173.10.26.202 > /dev/nul 2>&1

From this I see two cron jobs are running under the user <root>. The first updates the system clock every four hours and the second updates AWStats three times a day.

Although it looks difficult to interpret the output from crontab -l it really is fairly simple.

Within a cron job there a six columns or spacings which represent the following arguments:

  • Argument 1: Minute (0 – 59)
  • Argument 2: Hour (0 – 23)
  • Argument 3: Day of Month (1 – 31)
  • Argument 4: Month (1-12)
  • Argument 5: Day of Week (0 – 6) Sunday = 0
  • Argument 6: Command

 

Going back to our example of the rdate cron job:

0 0,4,8,12,16,20 * * * rdate -s clock.xmission.com > /dev/nul 2>&1

The first column contains a 0 which represents :00 on a clock time.

The second column actually shows six entries separated by commas 0,4,8,12,16,20. This indicates the script is to run at 12:00 midnight, 4:00 am and so forth using a 24 hour clock.

I have an asterisk representing the third column. An asterisk is used to exclude nothing or in this case, the script will run on all days of the month.

Columns four and five also display an asterisk indicating the script will run every month and every day of the week.

The sixth and last column is your actual scripted command. In my case I am using ”’rdate”’ to update my system clock.

At the end of my command I have included > /dev/nul 2>&1. Whenever a script runs without this addition, an email is sent to the user on the system running the command. Since I don’t wish for root to be receiving repetitive emails indicating the completion of this command, adding > /dev/nul 2>1 will send the output to the bit bucket.

When you are ready to create your own cron job, you can use the following command:

[root@web ~]# crontab -e

This will put you in edit mode using vi as an editor to create your new job.

When completed and saved, your will see the following message indicating a successfully created cron job.

crontab: installing new crontab

LEAVE A COMMENT