anacron is similar to cron, and it works much the same way. anacron is intended to run jobs in /etc/crontab that cron missed due to system downtime. If a Linux box is left on continuously, anacron won't run any jobs because cron will run them. anacron has its own crontab file, /etc/anacrontab. Changes made to /etc/crontab need to be made in /etc/anacrontab as well. anacron is a utility for root only. There is no anacrontab command to edit the file for users. The system administrator should edit the file manually. Here is the anacrontab supplied with anacron on Red Hat systems:
# /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 1 65 cron.daily run-parts /etc/cron.daily 7 70 cron.weekly run-parts /etc/cron.weekly 30 75 cron.monthly run-parts /etc/cron.monthly
The format is similar to the crontab files, but there are some differences. The first few lines set the shell and path. The remaining lines contain the scripts to execute; anacron calls these job description lines.
The first field of the job description line indicates how many days should occur between runs of the process. The second field indicates how many minutes to wait before starting the process. The third field is the job identifier. The last field and remainder of the line is the shell command to execute.
See the anacrontab(8) man page for further details.
The anacrontab file doesn't execute run-parts /etc/cron.hourly because the smallest time unit for anacron is one day.
There is a potential problem with the previous anacrontab file. The full path to the run-parts script is not given. The run-parts script is in /usr/bin. The /usr/local/sbin and /usr/local/bin directories are searched before /usr/bin according to the PATH variable. If users have write permission to /usr/local/sbin or /usr/local/bin, a script called run-parts could be placed in those directories and would run instead of the intended /usr/bin/run-parts. This issue can be prevented by always using the full path for scripts in anacrontab. For example:
# /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 1 65 cron.daily /usr/bin/run-parts /etc/cron.daily 7 70 cron.weekly /usr/bin/run-parts /etc/cron.weekly 30 75 cron.monthly /usr/bin/run-parts /etc/cron.monthly
anacron is started by a startup script.
#chkconfig --list anacron anacron 0:off 1:off 2:on 3:on 4:on 5:on 6:off
anacron checks the timestamp files in /var/spool/anacron and runs any jobs that were missed.
# ls -al /var/spool/anacron total 20 drwxr-xr-x 2 root root 4096 Nov 6 2003 . drwxr-xr-x 16 root root 4096 Nov 6 2003 .. -rw------- 1 root root 9 Aug 14 12:53 cron.daily -rw------- 1 root root 9 Aug 1 04:42 cron.monthly -rw------- 1 root root 9 Aug 8 04:22 cron.weekly
anacron records whether cron is running normally so that jobs are not run twice. This is done with an anacron script in /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly, which is executed by cron. The anacron -u command is run by this anacron script to update the timestamps and indicate that all jobs were run for that day by cron.
anacron messages route to the same log file as cron messages. anacron logs messages using syslogd with the facility value set to cron. Here are some sample anacron log entries:
Aug 18 10:43:58 sawnee anacron: Job 'cron.daily' started Aug 18 10:43:58 sawnee anacron: Updated timestamp for job 'cron.daily' to 2004-08-18
The following files are delivered with anacron:
# rpm -q --filesbypkg anacron-2.3-25 anacron /etc/anacrontab anacron /etc/cron.daily/0anacron anacron /etc/cron.monthly/0anacron anacron /etc/cron.weekly/0anacron anacron /etc/rc.d/init.d/anacron anacron /usr/sbin/anacron anacron /usr/share/doc/anacron-2.3 anacron /usr/share/doc/anacron-2.3/COPYING anacron /usr/share/doc/anacron-2.3/README anacron /usr/share/man/man5/anacrontab.5.gz anacron /usr/share/man/man8/anacron.8.gz anacron /var/spool/anacron