systemd timers

You can schedule tasks with timers. If systemd is compiled with the feature, it makes cron unnecessary.

Timer management

In a fully-installed systemd-enabled system, there are multiple ways to manage your timers.

While managing them is practically the same as any other units (see systemd), there are timer-specific ways to manage them easier.

Timedate formats

systemd has different ways to denote time.

To find more details about time notation, you can view the systemd.time.7 manual page.

Here's an example of setting a timer for an example backup service. The following timer unit sets it to execute every day at 18:00.

[Unit]
Description=A deduplicated backup from my computer
Documentation=man:borg(1) https://borgbackup.readthedocs.io/

[Timer]
Unit=borg-backup.service
OnCalendar=*-*-* 18:00:00
Persistent=true

[Install]
WantedBy=graphical.target

If the timer unit is started, this will trigger borg-backup.service from the load path. But you can omit Timer.Unit key if you named the timer unit file similarly (e.g., borg-backup.timer with borg-backup.service).

You can find more information about it from the systemd.timer.5 manual page. Furthermore, systemd has a testing tool for time with systemd-analyze {timespan,timestamp,calendar}.

printf "Timespan example:\n"
printf "..............\n"
systemd-analyze timespan 4000min
printf "..............\n\n"

printf "Timestamp example:\n"
printf "..............\n"
systemd-analyze timestamp 2021-07-01
printf "..............\n\n"

printf "Calendar example:\n"
printf "..............\n"
systemd-analyze calendar "*-1/4-5 0/2:00:00"
printf "..............\n\n"
Timespan example: .............. Original: 4000min μs: 240000000000 Human: 2d 18h 40min .............. Timestamp example: .............. Original form: 2021-07-01 Normalized form: Thu 2021-07-01 00:00:00 PST (in UTC): Wed 2021-06-30 16:00:00 UTC UNIX seconds: @1625068800 From now: 10 months 21 days ago .............. Calendar example: .............. Original form: *-1/4-5 0/2:00:00 Normalized form: *-01/4-05 00/2:00:00 Next elapse: Mon 2022-09-05 00:00:00 PST (in UTC): Sun 2022-09-04 16:00:00 UTC From now: 3 months 13 days left ..............

Backlinks