Go to TogaWare.com Home Page.
GNU/Linux Desktop Survival Guide
by Graham Williams
Google

CRON: Regular Tasks


GNU/Linux has a mechanism for running certain applications regularly through a daemon called crond.

Debian uses cron in a very neat and stylised fashion. Scripts to be run regularly are maintained in one of the directories /etc/cron.hourly/, /etc/cron.daily/, /etc/cron.weekly/, or /etc/cron.monthly/. The entries in /etc/crontab then simply run any scripts in these directories on a regular basis. The hourly scripts, by default, are run at 17 minutes past the hour, each hour. The daily scripts are run at 6:25am each day. The weekly scripts are run each Sunday at 6:47am and the monthly scripts are run at 6:52am on the first day of each month. The default /etc/crontab essentially contains just the following lines:



17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron \
           || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron \
           || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron \
           || ( cd / && run-parts --report /etc/cron.monthly )

The first line is read as: 17 minutes past every (*) hour on every day (*) on every month (*) and every day of the week (*), as user root run the rest of the line. The second line then reads as: 25 minutes past 6am on every day of the month and every month of the year and every day of the week as user root run the rest of the line. The ``rest of the line'' in this second case tests to see whether anacron is installed and if not runs the scripts found in /etc/cron.daily/. If anacron is installed then it is used instead to ensure the scripts are run. The anacron package is a replacement for cron that does not assume the local host is always powered on (Unix and GNU/Linux systems rarely require rebooting and are happy to remain powered on indefinitely, but laptops don't tend to remain on always) and will ensure the regular scripts still get run appropriately when the local host is powered on.

If the default times for cron don't suit (e.g., you like to work early in the morning and get annoyed by the sudden disk whirling each morning at 6:25 am) then simply change the times in /etc/crontab. Unlike traditional versions of cron, the newer versions do not require the running of the crontab command to install the new version when you edit /etc/crontab.

A user can easily add entries to their own crontab (stored in /var/spool/cron/crontabs through the crontab command. Use the -l option to list the current contents and the -e option to edit the crontab file.

When anacron is installed (as it might be for a machine that is not always turned on), cron will start it up at 7:30am each day (through the settings in /etc/cron.d/anacron) and thus it will run its daily jobs at 7:35am (given the defaults in /etc/anacrontab). Chron will also then hand over the running of regular jobs in /etc/cron.daily/, /etc/cron.weekly/, and /etc/cron.monthly/ over to anachron, as we see in the default /etc/contab above. To change this start time we can edit /etc/cron.d/anacron to specify the start time that we want. The package can be safely removed, if you like, and leave cron in charge, which is just fine for a desktop.

Note that changes to /etc/crontab and any files in /etc/cron.d (such as changing the time) will take effect immediately.

Copyright © 1995-2014 Togaware Pty Ltd
Support further development through the purchase of the PDF version of the book.
Brought to you by Togaware.