As an experienced Unix administrator it is sometimes easy to lose track of how mind boggling even the simplest tool can be for the novice administrator and it is for this reason that I though it might be useful to have a  laymans explanation of the crontab file and what it does.

Crontab is a program that is use to manipulate the CRON daemon, making it possible for administtrators and users to schedule and run tasks and/or programs at pre-determined times. Essentially the Crontab can be considered to be a flat text file that contains a list of commands that will be executed by the system on behalf of the user that owns that specific crontab file.

So, Why do I need a Crontab file?

It is not imperative for every user to have and/or use a crontab file, but as a system administrator it is an invaluable tool for automating, managing and scheduling repetetive administrative tasks, be they required daily, weekly, monthly or at highly specific periods, these can all be automated by utilising the crontab command.

OK, how do I use it then?

Whilst logged in as either the administrator (typically “root”) or alternatively as a particular user you can access the crontab with the following commands:

N.B: The optional

[Username] parameter can be used by the root (administrative user) to manipulate the crontab of any user

  • crontab -l [Username]
    This command will show a list of the current cron jobs
  • crontab -e [Username]
    This command will allow you to edit/add/remove items in  the list of current cron jobs
  • crontab -r [Username]
    This command will remove the crontab file (use with caution)
  • crontab -v [Username]
    This command will show the status of the crontab file and the date and time that it was last edited.

What does the crontab file look like?

So you have decided to edit the crontab file and you use the crontab -e command and your crontab will be opened using the vi editor unless you have altered your preferred editor. You should then be presented with a screen resembling the following:

#0 3 * * * /usr/sbin/skulker
#45 2 * * 0 /usr/lib/spell/compress
#45 23 * * * ulimit 5000; /usr/lib/smdemon.cleanu > /dev/null
0 11 * * * /usr/bin/errclear -d S,O 30
0 12 * * * /usr/bin/errclear -d H 90
0 15 * * *  /usr/lib/ras/dumpcheck >/dev/null 2>&1
30 13 12 * * _SUMA=cron /usr/suma/bin/suma -x 1
0 0 * * * /opt/csm/bin/cfmupdatenode -a 1>/dev/null 2>/dev/null
0 0 * * * /opt/csm/csmbin/cleanup.logs.csp  1>>/var/log/csm/csperror.log 2>>/var/log/csm/csperror.log

So what does it all mean?

You will need to condider the following syntax for any commands that you decide to add or edit.

0 12 * * * /usr/bin/errclear -d H 90

OK, lets use this to find out what the above line is telling us

  • the first 0 tells us the command will run at 00 minutes
  • the 12 tells us the the command will run at 12 hours
  • the * in the following 3 fields tell us the the command will run EVERY day, EVERY month and EVERY day of the week
  • /usr/bin/errclear -d H 90 is the command that will be run, which for those of you that are interested is a command to manipulate the error log (/usr/bin/errclear) to (-d) delete all (H) Hardware errors more than (90) ninety days old.

Multiples of any of the fields can be also be stipulated by seperating values with commas for example to run a command every day at 12:15, 12:30, 15:15 15:30 18:15 and 18:30 you would use the following syntax

15,30 12,15,18 * * * [command to be executed]

You can also specify inclusive ranges by seperating the numbers with a dash for example to run a command at midday between Monday and Friday you could use the following syntax:

00 12 * *  1-5 [command to be executed]

You will notice that some commands have the following string  >/dev/null 2>&1 or similar appended to the end of the command. The default action for a cron job is to send an email to the user to notify them that the command has been execute. Now, after initial testing you probably don’t want to be notifed every time that your cron job has been executed so you can append the >/dev/null 2>&1 string to the end of the file and thie email notification will not be sent.

Another *VERY IMPORTANT* fact to remember is that each cron command must be placed on a seperate line and it is always good practicce to put the full pathname to the command or script you are running.

When you are finished editing or adding your commands you will need to save the file.

Once you have saved the file ALWAYS use the crontab -l command to display the file and check the syntax of the command that you added or modified.

…because the Cron command can be a little difficult to grasp initially it is useful to learn by seeing examples, so here are a few to get you on your way.

Run the script every day at 12:00.
0 12 * * * /home/your_username/ >/dev/null 2>&1

Run the script every day at 12:00, 14:00 and 16:00.
0 12,14,16 * * * /home/your_username/ >/dev/null 2>&1

Run the script every Sunday at 13:30.
30 13 * * 0 /home/your_username/ >/dev/null 2>&1

Run the script every Saturday at 12:00, 14:00 and 16:00.
0 12,14,16 * * 6 /home/your_username/ >/dev/null 2>&1

Run the script on the first, fifteenth and twentieth of every month.
0 0 1,15,20 * * /home/your_username/ >/dev/null 2>&1

Run the script every day from 3 to 3 hours: 00:00, 03:00, 06:00 etc.
0 */3 * * * /home/your_username/ >/dev/null 2>&1

…so now you can go away and schedule all sorts of wonderful things to happen on your Unix and Linux systems.

Have fun!!!