These classnotes are depreciated. As of 2005, I no longer teach the classes. Notes will remain online for legacy purposes

UNIX01/System Wide Cronjobs

Classnotes | UNIX01 | RecentChanges | Preferences

While you could technically run any system-wide cronjobs from the root user's crontab (afterall, the root user has access to everything on the system), this is a very sloppy way to do things, and is generally frowned upon (the rare exception is in the embedded devices arena, where root's crontab is smaller than the norma system-wide setup).

Instead, cron offers a mechanism by which you can set up system-wide cronjobs to be run at very specific times. This is really where the cron daemon you are using can be vastly different from alternative cron daemons. Again, we will only be focusing on Vixie Cron and Anacron in this class, so be sure to check your documentation before applying what we learn here to an arbitrary UNIX system.

/etc/crontab

By far the simplest way to add a system-wide cronjob is to modify the entries in /etc/crontab. This file has the same format as the other crontab files we have examined thus far. The big differences for this system-wide crontab file is that there is an extra field for a user to run as and that you do not need to call "crontab" to reload the file (cron automatically reloads it whenever it wakes up).

However, looking inside of the /etc/crontab file on many Linux systems will give us something like the following:

 # /etc/crontab: system-wide crontab
 # Unlike any other crontab you don't have to run the `crontab'
 # command to install the new version when you edit this file.
 # This file also has a username field, that none of the other crontabs do.

 SHELL=/bin/sh
 PATH=/usr/local/sbin:/usr/local/bin: \
             /sbin:/bin:/usr/sbin:/usr/bin

 # m h dom mon dow user  command
 25 6    * * *   root    test -e /usr/sbin/anacron || \
        run-parts --report /etc/cron.daily
 47 6    * * 7   root    test -e /usr/sbin/anacron || \
        run-parts --report /etc/cron.weekly
 52 6    1 * *   root    test -e /usr/sbin/anacron || \
        run-parts --report /etc/cron.monthly

So what is this Anacron thing?

Anacron Overview

Anacron is a periodic command scheduler. It executes commands at intervals specified in days. Unlike cron, it does not assume that the system is running continuously. It can therefore be used to control the execution of daily, weekly and monthly jobs (or anything with a period of n days), on systems that don't run 24 hours a day. When installed and configured properly, Anacron will make sure that the commands are run at the specified intervals as closely as machine-uptime permits.

Every time Anacron is run, it reads a configuration file that specifies the jobs Anacron controls, and their periods in days. If a job wasn't executed in the last n days, where n is the period of that job, Anacron executes it. Anacron then records the date in a special timestamp file that it keeps for each job, so it can know when to run it again. When all the executed commands terminate, Anacron exits.

Why may Anacron be useful?

Most Unix-like systems have daily, weekly and monthly scripts that take care of various "housekeeping chores" such as log-rotation, updating the "locate" and "man" databases, etc. Daily scripts are usually scheduled as cron-jobs to execute around 1-7 AM. Weekly scripts are scheduled to run on Sundays. On machines that are turned off for the night or for the weekend, these scripts rarely get run.

Anacron solves this problem. These jobs can simply be scheduled as Anacron-jobs with periods of 1, 7 and 30 days.

Anacron is especially useful for laptop users and for people who only turn their computer on for a few hours a day.

What is Anacron not?

Anacron is not an attempt to make cron redundant. It cannot be used to schedule commands at intervals smaller than days. It also does not guarantee that the commands will be executed at any specific day or hour.

It isn't a full-time daemon. It has to be executed from boot scripts, from cron-jobs, or explicitly.

Anacron Directories

Anacron generally looks for scripts to run in the following directories:
 /etc/cron.daily
 /etc/cron.weekly
 /etc/cron.monthly

When it spawns for one of these settings, it runs sequentially through the scripts in these directories. Thus, if you had a script which needed to be run daily, provided the system was on, you could place that script inside the /etc/cron.daily directory and it would run.

Take a look at some of the scripts in these directories now.



Classnotes | UNIX01 | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited August 9, 2003 2:40 am (diff)
Search:
(C) Copyright 2003 Samuel Hart
Creative Commons License
This work is licensed under a Creative Commons License.