by pbharris

(Transferred from the wiki by Peter)


Created July 15, 2002

Okay - You want to run a program at a certain time everyday or perhaps every week and you don't want to have to remember to do it. Perhaps you wish to delete a bunch of temporary files, syncronize your computer or send out mail notifications. Maybe you hear people talking about cronjobs or crontab and you wish to know what it is
Crontab entries looks complex but they are really simple, infact they are as simple as shown below:
Code:
minute hour dayofmonth month dayofweek command_1;command_2;...command_n
The entries you may have seen look like this:
Code:
7 * * 3 0 cat ~/mail/inbox > ~/old_mail;gzip ~/old_mail
Well it looks intimidating for someone unfamilar with it, first there are several numbers, a fairly long UNIX command with a strange~ and then another command with another strange ~

Well the first five fields are times when they events are scheduled to occur and then come the commands. The first five fields are in order as shown below:

  1. minute 0-59
  2. hour 0-23 (midnight is zero, 11:00 PM is 23)
  3. dayofmonth 1 - 31
  4. month 1 - 12
  5. dayofweek 0 - 6 (0 is Sunday, 1 is Monday etc.)

Many times you will see crontab entries that have astericks (*) in them, these are wildcards, which means that events will occur at any time. If you wanted an command to run every minute you could by entering an crontab entry that looks like this:
Code:
* * * * * command
But that is pretty boring, lets have a command that a a bit more practical, one nice thing to do on a regular basis for me is backup files. I want to do this on a monthy basis at 3:00 AM and lets do this on the 15th of every month.
Here would be my crontab entery would look like this:
Code:
* 3 15 * * cd ~pbharris; tar -cf backup.tar *;gzip backup.tar
Here the hour is 3 AM and the day of month is the 15th and since there is an asterick at the other positions they not have any effect. The commands seperated by a semi-colon are as follows:

  1. cd to the user pbharris' home directory (hense the tilda ~
  2. create a tar (tape archive) file named backup.tar
  3. compress the tar file with gzip.

To create any crontab entry the comamnd is
Code:
crontab -e
and to view ones crontab entries the command is
Code:
crontab -l
Here is what one looks like, you can have as many as you like, each on seperate lines.
Code:
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.30483 installed on Mon Jul 15 22:21:01 2002)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
0 * * * * cd /home/pbharris/ghclient099_linux; ./ghclient.x -nice 19 > /dev/null 2> /dev/null
* 3 15 * * cd ~pbharris/docs/pets/; tar -cf backup.tar * ;gzip backup.tar
You can only edit and view your own crontab unless you are root.



I hope this has been helpful, if you have any suggestions, comments or see an error please let me know.