Cron jobs

Running Regular Jobs with Cron

If there is a script or program that you would like to run regularly, rather than trying to remember to do it yourself you can schedule a computer to run it for you. The system program that does this is called cron and the file that lists your scheduled jobs (cronjobs) is called your crontab (table of cron jobs). The command used to tell cron what your crontab contains is also called crontab.

Creating a cronjob

To schedule cronjobs, login to the computer that you wish to run the jobs on and run:crontab -e This will open an editor for your crontab. You then specify one cronjob per line in the file. The syntax of a cronjob line is not very obvious but is explained quite well in the manpage or by running the UNIX command: man 5 crontab There is an example crontab file at the end of the manual page which should help you create your own crontabs.

Your crontab will be stored in the file /var/spool/cron/crontabs/<username> on the computer where it is active.

Cronjobs that run when a computer boots

If you have been given permission to run long term processes (eg: daemons) on a computer here then in most cases you should set them to start running automatically as the computer boots. This is something you can do with a cronjab.

This is generally much more convenient than logging in to start them yourself. If you want someone else to look after such daemons, then defining a clear start mechanism in the account crontab makes their job much simpler.

You can setup a cronjob to run everytime the server starts up. Use a crontab line like this: @reboot /path/to/command arg1 arg2 …

Show your crontab

crontab -l

Deactivating cronjobs

To remove the whole crontab, simply run:crontab -r Or, edit the crontab (crontab -e) and remove those cronjob lines you do not want to run. You can prepend a comment character (#) rather than removing a line.

Different crontabs on different computers

Note that crontabs are machine-specific, so you can't see them from another computer. To keep track of your cronjobs, you can create a directory in your homedir called .crontabs or crontabs. Every night a script is run on CSE computers that checks the cronjobs and compares them to what is stored in your crontabs subdirectory. If necessary the crontab is copied across to your homedir (of course, you can copy it there yourself).

If the script finds differences between the crontab on the computer and the one in your homedir, you will be mailed.

Last edited by robertd 28/06/2015

Tags for this page:

cron