Click to return to home page
Richard Lowe Jr Home

Cron

Cron is a program on Unix (as well as Linux) servers which allows you to schedule jobs to run at specific intervals. This is very useful for automating repetitive tasks.

It's simple to set up a job to be run under Cron - you define a file called a crontab. 

In Unix, there is a master crontab file in:

/etc/crontab

User crontab files are defined in:

/var/spool/cron

with a file name which is the same as the user's login id. This allows each user to have his own unique set of cron job definitions.

On Linux, the setup is a little bit different. The /etc/crontab file is defined to automatically execute cron jobs at certain intervals based upon the directories below.

/etc/cron.hourly
/etc/cron.daily
/etc/cron.weekly
/etc/cron.monthly

Thus, all you need to do is put a link or an executable within the directory matching the desired interval. It will then be automatically run at those times.

Users can also create cron jobs of their own, but the procedure is somewhat different. Users do not edit cron files (those in /var/spool/cron) directly. Instead, they issue a program called crontab, which in turn manipulates the cron files. The ability for users to do this is controlled by the administrator using directories called /etc/cron.allow and /etc/cron.deny).

crontab [-u user  ] file Store the specified file as a crontab file. -u is the user.
crontab -l List current crontab.
crontab -r Remove current crontab
crontab -e Edit current crontab

Each crontab file contains comments (lines beginning with #) and schedules. A schedule is as follows:

minute hour day_of_month month day_of_week user command

minute Minute on which to run
hour Hour to run
day_of_month Day of each month to rnu
month Month of the year, use names
day_of_week Day of the week (0 or 7 is Sun, or use names)

Any time field may be an asterisk which means "first to last". For example, in the day_of_month field, it means run every day of the month.

Thus, you simply create your file and submit it to cron. Most crontab files contain simply one line of text, which is the cron definition (and perhaps some comments).


Unless otherwise noted, all photos and text is Copyright © Richard G Lowe, Jr.