Avatar (Fabio Alessandro Locati|Fale)'s blog
Replace cron with systemd timers

Replace cron with systemd timers

November 20, 2026

Modern Linux systems still have cron available, but I think people should stop using it. Don’t get me wrong, it works perfectly for what it was designed for, but I think it is suboptimal for the majority of tasks, and keeping scheduled tasks in multiple places is a very confusing way of handling them.

I think that in modern Linux systems, using Systemd to substitute cron is the best approach. In fact, Systemd can perform the same scheduled activities as cron. Still, it is integrated with all Systemd facilities, so the experience is better and execution is more robust. In fact, Systemd can order jobs after network initialization and require filesystems to be mounted. It also handles logging automatically, ensuring logs are consistent with other system logs and easier to find and consult when needed.

The facility in Systemd for cron-like capabilities is Systemd timers. A timer, though, does not work alone. The only thing that a timer does is to trigger a service unit. This means that, once we have written the unit that we want to trigger via a timer, we can also trigger it manually. It works the same way as the automatic trigger, including the same environment, users, etc., since those properties are in the service unit, not the trigger.

Let’s pick as an example the following Systemd service to perform a backup of an application:

# /etc/systemd/system/myapp-bkp.service
[Unit]
Description=MyApp backup
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/bin/myapp-backup

The command can be a script, a compiled program, or any other executable. This service will execute /usr/bin/myapp-backup and, once it finishes, terminate itself using the return code from the executed command to determine whether it succeeded or failed. Also, Systemd will write all output and errors to Journald without needing any additional configuration. It is important to notice that network-online.target orders startup but does not guarantee that a remote destination is reachable. The backup command must still handle that failure.

Our Systemd service might be a bit more complicated, so, for instance, it could stop and start the service it is backing up if it requires offline backups, with the following:

# /etc/systemd/system/myapp-bkp.service
[Unit]
Description=MyApp backup
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
ExecStartPre=/usr/bin/systemctl stop myapp
ExecStart=/usr/bin/myapp-backup
ExecStopPost=/usr/bin/systemctl start myapp

In this case, Systemd will first stop myapp, then perform /usr/bin/myapp-backup, and attempt to restart myapp even if the backup fails. This uses ExecStopPost because ExecStartPost would be skipped on backup failure. The assumption is that myapp should run afterward even if it was stopped beforehand. It is also possible to manage backup retention in ExecStartPost, so our unit could be modified in:

# /etc/systemd/system/myapp-bkp.service
[Unit]
Description=MyApp backup
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
ExecStartPre=/usr/bin/systemctl stop myapp
ExecStart=/usr/bin/myapp-backup
ExecStopPost=/usr/bin/systemctl start myapp
ExecStartPost=/bin/bash -o pipefail -c 'find /bkp -maxdepth 1 -type f -name "myapp_*" -printf "%%T@ %%p\\0" | sort -z -n | head -z -n -10 | cut -z -d " " -f 2- | xargs -0 -r rm --'

The ExecStartPost entry will only keep the last 10 regular backups matching myapp_* by modification time, after a successful backup. Obviously, those are just examples, and you can easily tweak them to match your needs.

Once we have created the service unit with the required logic, we can switch to creating the timer.

A typical example is that the backup needs to be triggered every night at a certain time, such as 01:00. In this case, we can create the following:

# /etc/systemd/system/myapp-bkp.timer
[Unit]
Description=Run the myapp backup every day

[Timer]
OnCalendar=*-*-* 01:00:00

[Install]
WantedBy=timers.target

As you can see, this is a very simple timer. The three asterisks mean any year, month, and day, similar to how cron works. The time is fixed at 01:00:00 in the machine’s local timezone. Systemd timers allow the usage of expressions such as Mon..Fri 02:00, or hourly, though I’m personally more used to the cron style, I recognize that sometimes it is easier to express certain periodicities this way.

One aspect that can be confusing about the above timer is how it knows which unit to trigger. You can specify Unit=myapp-bkp.service, but in this case it is implied, since the timer name matches the unit name.

The above example is similar to the equivalent cron schedule, with the caveat that activation happens within the minute, not at the exact second. However, you can tweak this by modifying the AccuracySec parameter. There are, though, some very interesting features that Systemd timers have that can make your tasks much easier to handle.

An example is Persistent=true, which ensures that if the machine was down when the timer should have triggered the service, the timer will run as soon as it can. This helps prevent situations where the machine is offline at the scheduled time, and the backup (or any other scheduled job) is missed. For calendar timers, it catches up once, and it does not replay every missed run or retry a failed job.

Another interesting feature is RandomizedDelaySec=15m, which allows you to randomize a delay, different at each execution, from the OnCalendar time to the time when the execution will really happen, in this case, a maximum of 15 minutes. This is very useful if you have multiple actions going at the same time and you are trying to avoid load spikes.

Adding those options, this is how the timer unit would look:

# /etc/systemd/system/myapp-bkp.timer
[Unit]
Description=Run the myapp backup every day

[Timer]
OnCalendar=*-*-* 01:00:00
Persistent=true
Unit=myapp-bkp.service
RandomizedDelaySec=15m

[Install]
WantedBy=timers.target

Systemd timers allow the use of OnBootSec instead of OnCalendar. This allows you to execute timers a specified time after boot. An example could be:

# /etc/systemd/system/myapp-bkp.timer
[Unit]
Description=Run the myapp backup once after boot

[Timer]
OnBootSec=15min

[Install]
WantedBy=timers.target

This unit will be scheduled 15 minutes after boot, or immediately if the timer is activated after that point, subject to the accuracy window. Doing backups 15 minutes after boot might seem odd, but this feature can be very handy if you have a job that initializes a service, refreshes a local cache, or checks that a service is up and running.

The OnBootSec can also be combined with OnUnitActiveSec or OnUnitInactiveSec, such as:

# /etc/systemd/system/myapp-bkp.timer
[Unit]
Description=Run the myapp backup after boot and every six hours

[Timer]
OnBootSec=15min
OnUnitActiveSec=6h

[Install]
WantedBy=timers.target

Those options allow you to reschedule the trigger after a specified time (in our case, 6 hours) after the previous instance was started (with OnUnitActiveSec) or finished (with OnUnitInactiveSec). These options allow you to accommodate a lot of scheduling flexibility easily. An important difference to remember, and one I also find very useful, is that a timer does not start another instance. At the same time, its service is still active from a previous execution.

To make those two files known to Systemd, we need to first reload Systemd unit definitions with:

# systemctl daemon-reload

Then, to enable and start just the timer:

# systemctl enable --now myapp-bkp.timer

The enable option ensures that, after any future reboot, the timer will be enabled, while --now starts it in the current boot.

It is possible to see the next scheduled activation of the unit with:

# systemctl list-timers myapp-bkp.timer

To test the actual unit without waiting for the timer to trigger it, or to execute it manually, you can start the service directly:

# systemctl start myapp-bkp.service

At any time, you can check the status of the job and its logs with:

# systemctl status myapp-bkp.service
# journalctl -u myapp-bkp.service

Here we just discussed the most basic features of Systemd timers, while they have many more, as can be seen in the man page. Given that the vast majority of Linux non-embedded systems now use Systemd, I think it makes total sense to leverage its capabilities instead of staying stuck on tools that have served us well but are much less modern. Cron is a great example.