Title: Using anacron to run periodic tasks | |
Author: Solène | |
Date: 28 June 2023 | |
Tags: openbsd anacron | |
Description: In this article, you will learn how to use anacron to run | |
periodic tasks on workstations that are not on 24/7 | |
# Introduction | |
When you need to regularly run a program on your workstation that isn't | |
powered 24/7 or even not every day, you can't rely on cronjob for that | |
task. | |
Fortunately, there is a good old tool for this job (first release June | |
2000), it's called anacron and it will track when was the last time | |
each configured tasks have been running. | |
I'll use OpenBSD as an example for the setup, but it's easily adaptable | |
to any other Unix-like system. | |
Anacron official website | |
# Installation | |
The first step is to install the package `anacron`, this will provide | |
the program `/usr/local/sbin/anacron` we will use later. You can also | |
read OpenBSD specific setup instructions in | |
`/usr/local/share/doc/pkg-readmes/anacron`. | |
Configure root's crontab to run anacron at system boot, we will use the | |
flag `-d` to not run anacron as a daemon, and `-s` to run each task in | |
a sequence instead of in parallel. | |
The crontab entry would look like this: | |
``` | |
@reboot /usr/local/sbin/anacron -ds | |
``` | |
If your computer is occasionally on for a few days, anacron won't run | |
at all after the boot, so it would make sense to run it daily too just | |
in case: | |
``` | |
# at each boot | |
@reboot /usr/local/sbin/anacron -ds | |
# at 01h00 if the system is up | |
0 1 * * * /usr/local/sbin/anacron -ds | |
``` | |
# Anacron file format | |
Now, you will configure the tasks you want to run, and at which | |
frequency. This is configured in the file `/etc/anacrontab` using a | |
specific format, different from crontab. | |
There is a man page named `anacrontab` for official reference. | |
The format consists of the following ordered fields: | |
* the frequency in days at which the task should be started | |
* the delay in minutes after which the task should be started | |
* a readable name (used as an internal identifier) | |
* the command to run | |
I said it before but it's really important to understand, the purpose | |
of anacron is to run daily/weekly/monthly scripts on a system that | |
isn't always on, where cron wouldn't be reliable. | |
Usually, anacron is started at the system boot and run each task from | |
its anacrontab file, this is why a delay field is useful, you may not | |
want your backup to start immediately upon reboot, while the system is | |
still waiting to have a working network connection. | |
Some variables can be used like in crontab, the most important are | |
`PATH` and `MAILTO`. | |
Anacron keeps the last run date of each task in the directory | |
`/var/spool/anacron/` using the identifier field as a filename, it will | |
contain the last run date in the format YYYYMMDD. | |
# Example for OpenBSD periodic maintenance | |
I really like the example provided in the OpenBSD package. By default, | |
OpenBSD has some periodic tasks to run every day, week and month at | |
night, we can use anacron to run those maintenance scripts on our | |
workstations. | |
Edit `/etc/anacrontab` with the following content: | |
``` | |
SHELL=/bin/sh | |
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin | |
MAILTO="" | |
1 5 daily_maintenance /bin/sh /etc/daily | |
7 5 weekly_maintenance /bin/sh /etc/weekly | |
30 5 monthly_maintenance /bin/sh /etc/monthly | |
``` | |
You can manually run anacron if you want to check it's working instead | |
of waiting for a reboot, just type `doas anacron -ds`. | |
What does the example mean? | |
* every day, after 5 minutes (after anacron invokation) run `/bin/sh | |
/etc/daily` | |
* every 7 days, after 5 minutes, run `/bin/sh /etc/weekly` | |
* every 30 days, after 5 minutes, run `/bin/sh /etc/monthly` | |
# Useful examples | |
Here is a list of tasks I think useful to run regularly on a | |
workstation, that couldn't be handled by a cron job. | |
* Backups: you may want to have a backup every day, or every few days | |
* OpenBSD snapshot upgrade: use `sysupgrade -ns` every n days to | |
download the sets, they will be installed at the next boot | |
* OpenBSD packages update: use `pkg_add -u` every day | |
* OpenBSD system update: use `syspatch` every day | |
* Repositories update: keep your cloned git / fossil / cvs / svn | |
repository up to date without doing it aggressively | |
# Conclusion | |
Anacron is a simple and effective way to keep your periodic tasks done | |
even if you don't use your computer very often. |