Tutoriel Linux

Anacron under Linux: resuming a task when the machine was turned off

Débutant5 min de lecture
À retenirLinux n'est pas réservé aux experts. Le bon point de départ : une distribution accessible, une sauvegarde propre et quelques commandes comprises.

You schedule a backup for 2:00 AM, but the laptop is turned off at that time. With a standard cron job, the execution is simply lost. anacron responds to this specific case: it launches the task at the next startup as soon as it notices that its period has expired.

I reserve it for daily, weekly, or monthly tasks that can tolerate a delay. For a specific time, execution every ten minutes, or advanced monitoring by department, cron and systemd timers remain more suitable.

Tux restarts a computer and catches up on a scheduled backup with Anacron
Anacron executes a delayed task at the next startup when its timeout period has expired.

Choosing between cron, anacron, and a systemd timer

The choice depends primarily on the required accuracy and the machine’s start-up time:

  • cron suitable for a machine switched on at the scheduled time and for frequencies less than once a day;
  • anacron works in days and catches up on a daily, weekly or monthly task after a period of rest;
  • A systemd timer provides a more precise schedule Persistent=truedependencies and logs directly related to a service.

Anacron doesn’t replace cron everywhere. It doesn’t try to execute a command at exactly 8:30 AM. It checks if the specified number of days has passed, waits for the configured delay, runs the command, and then records the date. The time of the last execution is not used in the calculation.

systemd timers offers a more flexible solution for a new task.

Install Anacron and read the existing configuration

On Debian or Ubuntu, install the package and then check the available version:

sudo apt update

anacron -V

On Fedora, Rocky Linux, or AlmaLinux, the implementation is usually provided by the Cronie package family. Check the installed package before modifying anything:


command -v anacron

Next, display the main file without editing it:

sudo cat /etc/anacrontab
sudo ls -l /var/spool/anacron

The first file describes the tasks. The second location contains their timestamps. Depending on the distribution, anacron can be triggered by cron, a systemd service, or at startup. Check what actually exists:

systemctl status anacron.service --no-pager
systemctl list-timers --all | grep -i anacron
sudo grep -R "anacron" /etc/cron.d /etc/cron.* 2>/dev/null

Add a daily task with a backup

Start by saving the configuration. An error in /etc/anacrontab may prevent the following tasks from being taken into account.

sudo cp /etc/anacrontab /etc/anacrontab.bak-$(date +%F-%H%M)
sudo nano /etc/anacrontab

For example, add this line to call a backup script that has already been tested:

1 15 backup-home /usr/local/sbin/backup-home.sh >> /var/log/backup-home.log 2>&1

The four fields each have a specific role:

  • one defines a period of one day;
  • fifteen adds fifteen minutes of delay after Anacron starts;
  • backup-home is the unique identifier used for timestamping and logging;
  • The last field contains the complete order.

Validate the syntax and then run a controlled test

First, test the script itself. It must be executable, produce the expected result, and return a non-zero code if it fails:

sudo test -x /usr/local/sbin/backup-home.sh
sudo /usr/local/sbin/backup-home.sh
echo $?

Next, validate the anacrontab file without running the task:

sudo anacron -T

The absence of output with a zero return code indicates that the syntax is accepted. To execute only the task backup-home without waiting for its deadline or period, use:

sudo anacron -f -n -d backup-home

This command actually launches the script. Do not test it with a delete or a synchronization. --delete or an irreversible operation. The options force the task, cancel the delay, and keep Anacron in the foreground to display its activity.

Find the execution details in the logs and timestamp

After the test, first check the business outcome, for example, the presence and date of the saved files. The anacron timestamp only confirms that the command was launched and completed.

sudo stat /var/spool/anacron/backup-home
sudo tail -n 80 /var/log/backup-home.log

On a distribution with a systemd unit, see also:

sudo journalctl -u anacron.service -n 80 --no-pager
sudo journalctl -t anacron -n 80 --no-pager

If these commands remain empty, check the distribution’s cron log, often /var/log/syslog on Debian or Ubuntu and about the RHEL family. The guide on journalctl and startup logs help to restore the triggering process after a boot.

One important detail to note: Anacron records the date when the command finishes, even if your script failed. It does not become an error recovery system. The script must log its result, check its prerequisites, and send an alert if the task is critical.

Limitations to be aware of before replacing a cron job

Do not use anacron for a task that must start at a specific minute. Its period is calculated in days, and the delay begins when anacron is triggered. After several days of downtime, each delayed ID is executed once, not once per missed day.

Do not move an existing task before identifying its user. /etc/anacrontab is generally processed with root privileges. A command copied from a user’s crontab can then create files with the wrong owner or access data it shouldn’t touch.

Anacron manual page documents the testing and execution options. Anacrontab manual Specify the fields for period, delay, identifier, and order. Keep the old cron job commented out for a full cycle, check the actual output of the task, and then delete it only when the catch-up process is working.

sudo apt update && sudo apt upgrade