[Linux](EN) systemd service timer example
Make systemd service which runs every 5 seconds by using timer.
Environment and Prerequisite
- Ubuntu 18.04
Goal
- Create timer example which is for using systemd service in specific date, time or event.
- systemd timer have options like
OnActiveSec=, OnBootSec=, OnStartupSec=, OnUnitActiveSec=, OnUnitInactiveSec=
andOnCalendar=
. This post example useOnCalendar=
systemd service and timer
systemd
systemd
: According to Wikipedia, systemd is a software suite that provides an array of system components for Linux operating systems.
service
service
: A unit configuration file whose name ends in “.service” encodes information about a process controlled and supervised by systemd.
timer
timer
: A unit configuration file in systemd which is for using service in specific date, time or event.
systemd service timer file example
Necessary files
- We need service and timer file.
- service which is run by timer should have same file name with that timer
- In this example, each name is
on-calendar.service
andon-calendar.timer
on-calendar.service
[Unit]
Description=Test for OnCalendar timer
[Service]
Type=oneshot
ExecStart=/usr/bin/test_on_calendar.sh
[Install]
WantedBy=multi-user.target
Description
: Description of this fileType
: It is type of service which can hassimple
,oneshot
,forking
or etc. This example useoneshot
.oneshot
is used for short-lived and one-off tasks because it blocks on operation and wait process to be finished. When option isoneshot
, then its service state will beactivating
while running and will be changed toinactive
after it.ExecStart
: Give absolute path of running file or program.WantedBy
: It is for specifying how a unit should be enabled. By usingsystemctl enable
command its symbolic link is added to.wants
directory under/etc/systemd/system
depending on its specified target. It will be run after booting depends on its.wants
/usr/bin/test_on_calendar.sh
- Script for running.
#!/bin/bash
date >> /tmp/OnCalendarLog.txt
- Give execution permission
chmod a+x /usr/bin/test_on_calendar.sh
on-calendar.timer
[Unit]
Description=Test for OnCalendar timer
[Timer]
OnCalendar=*-*-* *:*:0/5
AccuracySec=1s
[Install]
WantedBy=timers.target
Description
: Description of this fileOnCalendar
: This options specifies when to run or how frequently run. Its format isYEAR-MONTH-DAY HOUR:MINUTE:SECOND
. If you like to run service in every n seconds, just make it to*-*-* *:*:0/n
. It is also same in hour or minute. It can be used in many various ways.AccuracySec
: This unit is used to set the level of accuracy with which the timer should be adhered to. Its default value is 1 minute. So if you want to run timer in second, you need to setAccuracySec=1s
like above example.WantedBy
: It is for specifying how a unit should be enabled. By usingsystemctl enable
command its symbolic link is added to.wants
directory under/etc/systemd/system
depending on its specified target. It will be run after booting depends on its.wants
systemd service timer usage example
Make it to enable
$ systemctl enable on-calendar.timer
systemd timer start
$ systemctl restart on-calendar.timer
timer list check
$ systemctl list-timers
NEXT LEFT LAST PASSED UNIT ACTIVATES
Sun 2020-02-09 00:02:35 KST 1s left Sun 2020-02-09 00:02:30 KST 2s ago on-calendar.timer on-calendar.service
Example result
$ cat /tmp/OnCalendarLog.txt
Sun Feb 9 00:02:10 KST 2020
Sun Feb 9 00:02:15 KST 2020
Sun Feb 9 00:02:20 KST 2020
Sun Feb 9 00:02:25 KST 2020
Sun Feb 9 00:02:30 KST 2020
Reference
- https://ko.wikipedia.org/wiki/Systemd
- https://www.freedesktop.org/software/systemd/man/systemd.timer.html
- https://www.freedesktop.org/software/systemd/man/systemd.service.html
- https://unix.stackexchange.com/questions/506347/why-do-most-systemd-examples-contain-wantedby-multi-user-target
- https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files