cron scripts being executed at wrong time

Feb 4, 2010
Checked on: Ubuntu Karmic Koala 9.10

Few days back I was shocked to see that one of my cron scripts was being executed at 3:24PM, which was actually scheduled to be executed at 4AM. Probing with the system logs further revealed that none of the cron scripts were following the specified schedule. After googling a little bit I found that it is a common problem and has been discussed in many forums. Here is a synopsis of the all the discussions.

Cron is a utility, which is supposed to automatically execute specific commands periodically (daily, monthly etc). The preferences of the schedule for root is stored in /etc/crontab file and can be edited directly to make changes. Check this link to understand structure of crontab. Non-root users can also have their own cron schedule but their crontab file are located at different location. So, non-root users are suggested to use following command for editing their crontab. crontab -e Default entry of /etc/crontab is given below: # /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 # and files in /etc/cron.d. These files also have username fields, # 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 17 *    * * *    root    cd / && run-parts --report /etc/cron.hourly 25 4    * * *    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 ) # Let us examine following line from crontab. 25 4    * * *    root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) It means that cron will execute following command daily at 04:25 AM as root. test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) The problem lies in first part of the command i.e. test -x /usr/sbin/anacron. The test command checks that whether the file /usr/sbin/anacron exists (& is executable), and if it exists then do not execute the next part (where actually scripts are being called). So, crontab is actually instructing to skip the execution of scripts at the scheduled time, if anacron is installed !! Sounds funny :) Clearly, this anacron is the culprit of all the chaos in schedule.

Anacron performs periodic command scheduling but without assuming that the system is running continuously. Cron schedules are not executed if the system is not running at the scheduled time. Anacron makes sure that these commands are run at the specified intervals as closely as machine uptime permits. Anacron reads a configuration file (/etc/anacrontab) 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. So, when a schedule is missed, the job is execute after next boot and a timestamp (depending on period of the job) is fixed for next run. This process may ultimately lead to timestamps which are too much out of the cron schedule. And this creates all the chaos.

Anacron is cron-complement (it requires cron) for laptop and home PC which are not running 24x7. So, Ubuntu Desktop edition have anacron installed by default and the crontab is configured so that cron is over ridden by anacron, if installed. The test commands can be removed from crontab as given below, if this default behavior is not required.

Note:
1. There are some known issues with hardware-clock time, when timezone is different. Check last few sections of this page.
2. fcron is intended to provide better features for periodic command scheduling.