Last Updated on
The cron daemon helps in running jobs at predefined times periodically. However, the system needs to running at all times so that cron can can execute a job at the scheduled time. This condition is well met by servers that run 24*7 hours a week during the year. But it is too much to expect this from personal laptop or desktop computers, which are switched off for at least few hours a day. For these machines, cron is not sufficient. There is another program, anacron, which along with cron provides the desired functionality of running jobs once a day, or week or a month even if the computer is switched off some time during the day.
anacron is driven by the file, /etc/anacrontab. A sample anacrontab file is,
# /etc/anacrontab: configuration file for anacron # See anacron(8) and anacrontab(5) for details. SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # These replace cron's entries 1 5 cron.daily nice run-parts --report /etc/cron.daily 7 10 cron.weekly nice run-parts --report /etc/cron.weekly @monthly 15 cron.monthly nice run-parts --report /etc/cron.monthly
anacrontab has to be edited by a user with superuser privileges; there is no crontab like command. Similar to a crontab, there are lines for comments and setting of environment variables. There should be no space on either side of the
= sign, unless space on the right side is intended in the value of the environment variable.
The format of the command lines in the anacrontab file is either of the following two lines,
period delay job-identifier command @period_name delay job-identifier command
The period is the number of days in which the command needs to be executed. Instead of period, @period_name can be given. The @period_name can only have the value @monthly. Since there are 28, 29, 30 or 31 days in different months, it is difficult to use number of days for scheduling a command once in a month. So, @monthly period_name comes handy in scheduling commands once in a month. The delay is in minutes. anacron waits for these many minutes before starting the command. The job identifier is a string and can not have a space or a slash. The command is any command recognized by the shell.
When anacron is invoked, it goes through the /etc/anacrontab file. For each job in the anacrontab file, it figures out when it had been run last. anacron keeps this record for each job in the /var/spool/anacron directory. Then, looking at the number of days parameter in command line, it works out whether it is to be run on the current day. If required, it runs the command. Then, it proceeds to the next command and so on. In the anacrontab file shown above, anacron is executing run-parts on the directory /etc/cron.daily, once in a day, executing run-parts on the directory /etc/cron.weekly once in 7 days and is executing run-parts on the directory /etc/cron.monthly once in a month.
On Ubuntu Linux systems, anacron is started at following times:
- As system startup the Upstart scrip /etc/init/anacron.conf,
anacron -sis run. The
-soption causes anacron to run jobs sequentially; it waits for the job at hand to complete before starting a new one.
- There is a crontab named anacron in /etc/cron.d. As per the crontab entry, cron starts anacron service at 7:30 A.M.
- There is a script /etc/apm/event.d/anacron, which starts the anacron service whenever the system is plugged into AC power or is woken up.
Unlike cron, anacron is not a daemon. When there is no job to do, anacron exits.
anacron and /etc/crontab
As per /etc/crontab on Ubuntu Linux systems, if anacron is not installed (cron checks whether /usr/sbin/anacron is executable), cron executes run-parts for /etc/cron.daily at 6:25 AM. Similary, if anacron is not there, it executes run-parts for /etc/cron.weekly at 6:47 AM on Sundays and run-parts for /etc/cron.monthly on first of a month at 6:52 AM.
Both anacron and cron use run-parts to execute the scripts in cron.daily, cron.weekly and cron.monthly. The syntax of run-parts is,
run-parts [--test] [--verbose] [--report] [--lsbsysinit] [--regex=RE] [--umask=umask] [--arg=argument] [--exit-on-error] [--help] [--version] [--list] [--reverse] [--] DIRECTORY
if neither --lsbsysinit nor --regex option is used, run-parts will only execute scripts with filenames having lowercase and uppercase alphabetical characters, digits, underscore and hyphen. So if there is a script with filename like
backup.sh, it will not be executed. If --lsbsysinit option is used, the script names in the directory must as per LSB namespaces. This means LANANA-assigned namespace containing alphabetic characters and digits (^[a-z0-9]+$), LSB hierarchical and reserved namespace optionally starting with underscore and containing owners DNS name in lowercase (^_?([a-z0-9_.]+-)+[a-z0-9]+$) or the cron script namespace containing lowercase and uppercase alphabetical characters, digits, underscore and hyphen. (^[a-zA-Z0-9_-]+$). If --regex option is used, the filenames must match the regular expression. In fact, with --regex, one can have filenames with periods and extensions like
.sh. So if there is script like
backup.sh in /etc/cron.daily, the anacontab entry for cron.daily can be modified,
1 5 cron.daily nice run-parts --regex='(.*)' --report /etc/cron.daily
The recommendation is to have the script filenames contain lowercase alphabetical characters and digits only (^[a-z0-9]+$).