Adam Prescott

Asynchronous job processing with dcron

Over the last few months I’ve been thinking a lot about how I’ve got backups set up. It’s not quite where I want it to be yet — trying to replace an unencrypted cloud service with a private, encrypted one and have it all automated is turning out to be a bit non-trivial, surprisingly — but I discovered a simple solution to a common problem when I first got the basics in place: how to handle a machine being restarted.

This isn’t actually anything new (it’s all over Google for [asynchronous cron] and the cron page of the Arch Linux wiki), but for some reason it took me a while to learn about it.

In any backup setup, there will be a daily task that needs to run. It might be expensive, so maybe you schedule it in the evening at, say, 9pm, when maybe you’re not doing much. This is great if your laptop is always on at 9pm, but what if it so happens that it’s not and your daily backups don’t run for a week?

One implementation of cron, dcron, supports an asynchronous format. Where your usual job might be:

0 21 * * *    run_daily_backup

You can instead use:

0 * * * *    ID=daily_backup FREQ=1d    run_daily_backup

The FREQ part of 1d means that run_daily_backup will execute as long as it’s been at least 1 day since it was last run. The way that two jobs are distinguished is with the ID part. So in this case, if the job with ID equal to daily_backup hasn’t been run in a day, the job will execute, otherwise nothing will happen.

Note that the entire job itself uses 0 * * * * as the cron expression, so that every hour there’ll be a check to see if the daily_backup job has been run. Your laptop only has to be turned on to see a new hour of the day, and the daily backup task will start.

This FREQ format is actually a more general form of the @frequency syntax:

@hourly    ID=hourly_task    some_hourly_task

This would be the same as FREQ=1h.

So this extra little feature basically avoids having to make your individual scripts aware of timestamps and time checking. All taken care of by dcron.

dcron aims to replace Anacron, which has some limitations. If you’re looking for something less minimal than dcron, there’s fcron, which has a few extra bells and whistles.