Cron jobs

Running Regular Jobs with Cron

If there is a script or program that you would like to run regularly, 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 (cron jobs) is called your crontab.

The command used to show, edit or remove your crontab is also called crontab.

Creating a cronjob

To schedule cron jobs, login to the computer that you want to run the jobs on and run crontab -e to edit your crontab.

You then specify one cron job per line in the file.

The syntax of a cron job line is not very obvious but is explained quite well in the manpage or by running man 5 crontab.

There's 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 permission to run daemons or other long term processes on a computer here, you probably want them to run automatically at boot time.

This is something you can do with a cron job, and is usually much less hassle than logging in to start them yourself.

Having the startup script in the account crontab also makes it much easier for CSG or other people to start or stop your daemons for you.

To get a job to run when the server starts up, add a line like this to the crontab:

@reboot /path/to/command arg1 arg2 …

Show your crontab

To see the contents of your current crontab file, run:crontab -l

Deactivating cronjobs

To remove the whole crontab, simply run:crontab -r

Alternatively, edit your crontab (crontab -e) and remove the entries you don't want.

You can also 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 crontabs, you can create a directory in your homedir called .crontabs or crontabs.

Every night a script is run on CSE computers that checks the crontabs and compares them to what is stored in your crontabs subdirectory.

If not found there, the crontab is copied across to your homedir.

If the script finds differences between the crontab on the computer and the one in your homedir, it will send you mail about it.

Last edited by robertd 22/05/2018

Tags for this page: