💾 Archived View for perso.pw › blog › articles › anacron.gmi captured on 2023-07-22 at 16:17:49. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-07-10)

➡️ Next capture (2024-02-05)

-=-=-=-=-=-=-

Using anacron to run periodic tasks

on Mastodon

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:

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?

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.

Conclusion

Anacron is a simple and effective way to keep your periodic tasks done even if you don't use your computer very often.