How to Use Cron

Cron is a utility that allows you to run programs at a specified time, such as backups. In my case, I wanted to have the server's date and time syncronized once a day with hstlxe (Hi-Stat's time server). Here is how it is done.

As the root, type in the following command: crontab -e This will open up the VI editor. This file configures cron and tells it when to run programs using the following convention:

There are normally
seven fields in one entry. The fields are:

minute hour dom month dow user cmd

minute This controls what minute of the hour the command will run on,and is between '0' and '59'


hour This controls what hour the command will run on, and is specified in the 24 hour clock, values must be between 0 and 23 (0 is midnight)


dom This is the Day of Month, that you want the command run on, e.g. to run a command on the 19th of each month, the dom would be 19.

month This is the month a specified command will run on, it may be specified numerically (0-12), or as the name of the month (e.g. May)

dow This is the Day of Week that you want a command to be run on, it can also be numeric (0-7) or as the name of the day (e.g. sun).

user This is the user who runs the command. Can be omitted

cmd This is the command that you want run. This field may contain multiple words or spaces.

You can use the asterick (*) as a wildcard.

Examples:


01 * * * * echo "This command is run at one min past every hour"
17 8 * * * echo "This command is run daily at 8:17 am"
17 20 * * * echo "This command is run daily at 8:17 pm"
00 4 * * 0 echo "This command is run at 4 am every Sunday"
42 4 1 * * echo "This command is run 4:42 am every 1st of the month"
01 * 19 07 * echo "This command is run hourly on the 19th of July"

Notes:

Under dow 0 and 7 are both Sunday.

 

In my example, I wanted to syncronize tht server's time with Hi-Stat's time server at 11:58 AM every day using the rdate command. To do this, I added the following line in my file:

58 11 * * * rdate -s hstlxe.lex.histat.com

When you exit the editor, it will submit the file to cron, and will execute at the prescribed time.

Other crontab commands are as follows:

Another example is using ntpdate. This command goes out on the internet and get time from a time server. An example of its usage in cron is below:

22 11 * * * ntpdate -u 193.67.79.202
00 00 * * * /sbin/clock -w

The above example sets the time with the ntp0.nl.net server (on port 37) at 22 minutes after 11 am. The second line sets the hardware clock to the system time at midnite (00 00)