SystemD - Timer

> Operating System - Kernel (Windows, Unix, Linux) > Linux / Unix > Linux - Boot (Init service system)

1 - About

To start a service at a scheduled time

Doc: systemd.timer

2 - Management

2.1 - Start

The matching .service is:

  • by default the name of the timer (e.g. foo.service for foo.timer)
  • or is specified in the [Timer] section by the Unit= option

Example:

  • Realtime timer (Timer units with at least one OnCalendar= directive will have an additional After= dependency on time-sync.target to avoid being started before the system clock has been correctly set.)
logrotate.timer
[Unit]
Description=Hourly rotation of log files
Documentation=man:logrotate(8) man:logrotate.conf(5)
 
[Install]
WantedBy=timers.target
 
[Timer]
OnCalendar=hourly
Persistent=true
AccuracySec=1min
  • for a Monotonic timer the timer section should be someting like that
logrotate.timer
[Timer]
OnBootSec=5min
OnUnitActiveSec=30min
  • Copy the unit file and reload the daemon
cp logrotate.timer /lib/systemd/system/
sudo systemctl daemon-reload
  • Enable
sudo systemctl enable logrotate.timer
Created symlink from /etc/systemd/system/timers.target.wants/logrotate.timer to /usr/lib/systemd/system/logrotate.timer.
  • Start
sudo systemctl start logrotate.timer
Advertising

2.2 - Status

sudo systemctl status logrotate.timer

2.3 - List

  • Only active
sudo systemctl list-timers
NEXT                         LEFT       LAST                         PASSED       UNIT                         ACTIVATES
Thu 2019-03-07 13:47:23 UTC  29min left Thu 2019-03-07 13:17:23 UTC  21s ago      logrotate.timer              logrotate.service
  • all timers (including inactive)
sudo systemctl list-timers --all

2.4 - Log

sudo journalctl -u logrotate.timer
os/linux/init/systemd_timer.txt · Last modified: 2019/03/07 15:35 by gerardnico