Systemd - timers examples
Timer configuration options
As an addition to the previous article describing timers in systemd, below I present several configuration possibilities.
In the [Timer]
section, the following options are available for defining a schedule:
OnCalendar
Calendar schedule (date and time). The syntax is e.g., DayOfWeek Year-Month-Day Hour:Minute:Second
.
You can use wildcards *
, ranges (Mon..Fri
), lists (e.g., Mon,Wed,Fri
), or special terms (@daily
, @hourly
, etc., e.g., OnCalendar=*-*-* 00:00:00
for daily at midnight). This option allows you to set any date or time.
Example:
- `OnCalendar=Mon..Sun *-*-* 05:00:00` – 5:00 AM every day of the week.
- `OnCalendar=Fri *-*-* 18:00:00` – Friday at 6:00 PM.
Protip
You can add multiple `[Timer]` entries in a single file for different times (e.g., different times on workdays and weekends).
Test the syntax with the command `systemd-analyze calendar "<expression>"`, which will show the next occurrences.
OnBootSec
Time (e.g., 5min
, 1h
, 30s
) after system startup. The timer triggers the service a specified amount of time after the system boots. This corresponds to the @reboot
construct from cron (in the example of migrating cron to timer, @reboot
→ OnBootSec=1s
).
OnStartupSec
Time after starting the systemd service manager. For system services, this is usually almost the same as OnBootSec
. It can be useful in user services, which start only when the user logs in.
OnUnitActiveSec
Time since the last (previous) activation of the associated service. This allows you to, for example, cyclically repeat a task at specified intervals, counting from the completion of the previous invocation.
OnUnitInactiveSec
Time since the deactivation (previous completion) of the associated service. Similar to the above, but counted from the moment the service ends, not when it starts.
Persistent
Logical option (true/false
). If set to true
, the timer will make up for missed invocations after system restart. By default, Persistent
is disabled
Example usage:
AccuracySec
Invocation precision time, default is 1min
. A timer scheduled for a specific hour may be run in a window from that hour to +AccuracySec
, to align wake-ups and save energy. The smaller the value (e.g., 1us
), the greater the accuracy.
RandomizedDelaySec
Random delay added to the times defined above, to prevent multiple timers from waking up simultaneously. Default is 0
. You can set a value when you need to distribute the load. Useful in environments shared among multiple users.
Application Examples
Several practical examples:
Automatic Backups
For example, every night at 3:00 AM. Let's create a backup.service
:
# /etc/systemd/system/backup.service
[Unit]
Description=My backup service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
Then a timer file backup.timer
:
# /etc/systemd/system/backup.timer
[Unit]
Description=Daily backup
[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true
Unit=backup.service
[Install]
WantedBy=timers.target
This timer will run the backup daily at 3:00 AM. Thanks to Persistent=true
, execution will be made up after restart if the system was on after 3:00 AM.
Cleaning Temporary Directories
For example, clean the /tmp
directory daily at 1:00 AM. We create a clean.service
:
and a timer clean.timer
:
[Unit]
Description=Timer for cleaning /tmp
[Timer]
OnCalendar=daily
Persistent=true
Unit=clean.service
[Install]
WantedBy=timers.target
The expression OnCalendar=daily
is a shortcut for *-*-* 00:00:00
(midnight every day). The clean_tmp.sh
script can delete old files in /tmp
.
📬 Need something more?
Write to: chris@terminalzone.eu
I'll be happy to answer your questions and select a solution tailored to your needs!