[Linux] 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= and OnCalendar=. This post example use OnCalendar=


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 and on-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 file
  • Type : It is type of service which can has simple, oneshot, forking or etc. This example use oneshot. oneshot is used for short-lived and one-off tasks because it blocks on operation and wait process to be finished. When option is oneshot, then its service state will be activating while running and will be changed to inactive after it.
  • ExecStart : Give absolute path of running file or program.
  • WantedBy : It is for specifying how a unit should be enabled. By using systemctl 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 file
  • OnCalendar : This options specifies when to run or how frequently run. Its format is YEAR-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 set AccuracySec=1s like above example.
  • WantedBy : It is for specifying how a unit should be enabled. By using systemctl 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

[Python] 파이썬(Python) List Comprehensions

> 파이썬(Python) List Comprehensions에 대해 알아보자.## 환경- Linux- Python 3.X.X## 파이썬(Python) List Comprehensions### List Comprehensions```pythonre...… Continue reading