Using systemd Timers for Scheduled Tasks

On Linux systems, scheduled tasks are often associated with cron. It is simple, widely available, and still useful today. However, on modern Linux distributions that use systemd, there is another powerful option: systemd timers.

A systemd timer can run a service at a specific time, after boot, or at regular intervals. Compared with traditional cron jobs, systemd timers integrate better with the system service manager, provide better logging, and offer more flexible scheduling options.

This article explains how systemd timers work and how to set them up.

What Is a systemd Timer?

A systemd timer is a unit file that controls when another systemd unit should be activated. In most cases, the timer activates a .service unit.

A typical scheduled task uses two files:

/etc/systemd/system/example-task.service
/etc/systemd/system/example-task.timer

The .service file defines what to run. The .timer file defines when to run it. This separation makes systemd timers clean and easy to manage.

A Simple Example: Daily Backup

Suppose we want to run a backup script every day.

First, create the script:

sudo nano /usr/local/bin/daily-backup.sh

Example content:

#!/bin/bash
echo "Backup started at Thu Jun 11 11:22:43     2026" >> /var/log/daily-backup.log
tar -czf /tmp/etc-backup.tar.gz /etc
echo "Backup finished at Thu Jun 11 11:22:44     2026" >> /var/log/daily-backup.log

Make it executable:

sudo chmod +x /usr/local/bin/daily-backup.sh

Creating the Service Unit

Create the service file:

sudo nano /etc/systemd/system/daily-backup.service

Content:

[Unit]
Description=Daily backup task

[Service]
Type=oneshot
ExecStart=/usr/local/bin/daily-backup.sh

Type=oneshot means the service runs a command and exits. This is suitable for scheduled jobs such as backups, cleanup, or reports.

Creating the Timer Unit

Create the timer file:

sudo nano /etc/systemd/system/daily-backup.timer

Content:

[Unit]
Description=Run daily backup task

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

OnCalendar=daily tells systemd to run the service once per day. Persistent=true ensures that if the machine was off at the scheduled time, systemd will run the missed task after boot.

Enabling and Starting the Timer

Reload systemd and enable the timer:

sudo systemctl daemon-reload
sudo systemctl enable --now daily-backup.timer

Check whether it is active:

systemctl status daily-backup.timer

List all active timers:

systemctl list-timers

Checking Logs

Because the task runs as a systemd service, logs can be viewed with journalctl:

journalctl -u daily-backup.service

Follow in real time:

journalctl -u daily-backup.service -f

This is much more convenient than manually redirecting cron output or checking local mail.

Common Timer Options

Run After Boot

To run a task 5 minutes after boot:

OnBootSec=5min

Run Repeatedly

To run a task every 30 minutes:

OnBootSec=5min
OnUnitActiveSec=30min

OnUnitActiveSec means the timer will run again 30 minutes after the previous activation.

Run at a Specific Time

Every day at 03:30:

OnCalendar=*-*-* 03:30:00

Every Monday at 04:00:

OnCalendar=Mon 04:00

First day of every month:

OnCalendar=*-*-01 00:00:00

Manually Testing the Service

Before relying on a timer, test the service manually:

sudo systemctl start daily-backup.service
systemctl status daily-backup.service
journalctl -u daily-backup.service

Disabling and Removing a Timer

Stop and disable:

sudo systemctl disable --now daily-backup.timer

Remove completely:

sudo rm /etc/systemd/system/daily-backup.service
sudo rm /etc/systemd/system/daily-backup.timer
sudo systemctl daemon-reload

systemd Timer vs Cron

Cron is still a good choice for simple jobs, especially on minimal systems. However, systemd timers offer several advantages:

  • Better integration with system services
  • Easy log viewing through journalctl
  • Dependency on other systemd units
  • Missed-run recovery with Persistent=true
  • More flexible timing options
  • Clear separation between schedule and task logic

For servers that already use systemd, timers are often a cleaner and more maintainable solution.

Summary

systemd timers are a modern and powerful way to manage scheduled tasks on Linux. A basic timer only requires two files: a .service file for the task and a .timer file for the schedule. Once you understand this structure, systemd timers become easy to use and much easier to debug than traditional cron jobs.