Job Scheduling in Off-line Machines

Author: Mojahedul Hoque Abul Hasanat mojahed@citechco.net

This document describes a utility to schedule jobs for a computer that does not run continuously for 24 hours.

1. Introduction

If you are not a complete newbie in the Unix world, you know what cron is. cron is used to run commands at specific times automatically. Unfortunately cron was designed for machines that run for 24 hours non stop. Unix used to run in only big machines. Now, with the blessing of free unices like Linux and FreeBSD, Unix is spreading in the cheap PC world being used as a low cost work station OS. Most of these machines don't run (or rather don't need to be run) continuously. The problem with cron is, if you wanted to run a command (a "job" in Unix speak), say at 10:00am every day, and your machine is turned off for some reason at that time, that job will not run for that day.

2. Why schedule jobs?

You might ask, why would I need to schedule jobs? As you spend more time with Unix, you will discover more and more jobs that need to be run every day, every 3 days, every week etc. Some good examples: And there are many more. They can be done manually, but do you really trust yourself to run that critical backup every 3 days? And it is a great hassle to trim the various log files at various intervals. By now you should be convinced that job scheduling is much more important in Unix than Win/DOS.

3. The magic solution

On solution is to write an intelligent script that is run by cron every 10 minutes, the script would check if it had run the job within 24 hours (or 3 days) and run it only if it hadn't. This trick does solve the problem but it is not elegant. I used to put up with that, until I found anacron. This is a small utility that comes with Debian Linux. Instead of running a job at a specified time, it runs a job after a specified interval.

Unlike cron, anacron does not run continuously as a daemon. When run (from a startup file like /etc/rc.d/rc.local, or from cron if the machine runs continuously for more than 24 hours), it reads a configuration file /etc/anacrontab. This file lists the jobs to run and their intervals. A job is run if its interval is passed.

To give you an idea, here is a sample anacrontab file

#-------------------- 
# sample anacrontab 
#--------------------
7   2   updatedb   /usr/bin/updatedb
1   1   backup-1   /root/scripts/make-backup-1day.sh
3   5   backup-3   /root/scripts/make-backup-3day.sh

The first column is the interval in days. The second column is the number of minutes after which the job should be run, this time is counted from the start of anacron. This is to avoid running all the jobs at the same time and hogging the machine. The third column is the job ID, if the job makes an output, it will be mailed to root specifying this job ID. The fourth column, as you have probably guessed by now is the job to be run. anacron exits after all the jobs that passed their intervals have been run. And of course, anacron runs itself and the spawned jobs in the background. Lines starting with '#' are comments.

To make everything clear, the fourth line (starts with 7) will run the /usr/bin/updatedb command every 7 days, and anacron will wait for 2 minutes before running it.

4. Where to get anacron

If you run Debian Linux (1.3+) you already have it on the CD. Otherwise you can download it here anacron_2.0.1.orig.tar.gz . In case of any problem, here is a local copy.


Back to BDLUG home page

Started: May 23, 1999.
Last modified: May 28, 1999.