Evaggelos Balaskas - System Engineer

The sky above the port was the color of television, tuned to a dead channel

Blog
Posts
Wiki
About
Contact
rss.png twitter linkedin github gitlab profile for ebal on Stack Exchange

fluxbox keys »
  -  
« disabling pocket on firefox
Jul
17
2015
Timers in systemd
Posted by ebal at 18:52:43 in blog, planet_ellak, planet_Sysadmin, planet_fsfe

Systemd Timers

It’s time to see an example on timers in systemd.

Intro

Before we start, let’s clarify some things.

systemd’s Timers are units. Units are the simplest form of systemd files. Units are describing “when and if” a unit service “should or must” run, based on real or relative time.

  • A real time example is similar to a cron job entry. You can find out all the configuration settings/values for OnCalendar here.

  • A relative time example is more close to something like:
    “Run this unit service, ten minutes after boot, before this service and also that services must have already been started cause I am depend on them.”

 

List of systemd-timers

To view the declared timers on a systemd, run the below command:

$ systemctl list-timers

we can see all timers, even the in-active ones, with:

# systemctl list-timers --all

 

one simple example

ok, let’s start with an example.

I’ll use the /usr/local/bin directory to store my custom scripts, as this directory is in the PATH enviroment variable and I can run these scripts from anywhere.

Our systemd unit files, must be under the /etc/systemd/system/ directory.

 

Part One: The Script

As an example, the script will mount some volumes after boot time.
The basic script contents the below lines:

# cat /usr/local/bin/mount.volumes.sh

#!/bin/sh

/usr/bin/mount /mnt/backup
/usr/bin/mount /var/lib/docker

and make it executable:

# chmod +x /usr/local/bin/mount.volumes.sh

You can run this script, once or twice to see if everything goes as planned.

 

Part Two: The Service

Now it’s time to create a systemd service unit:

# vim /etc/systemd/system/mount.volumes.service

[Unit]
Description=Mount Backup & Docker Volume Service

[Service]
Type=simple
ExecStart=/usr/local/bin/mount.volumes.sh

[Install]
WantedBy=multi-user.target

 

Part Three: The Timer

Now it is time to create the systemd timer unit:

# vim /etc/systemd/system/mount.volumes.timer

We have to decide when we want to service to run.
eg. Every day, but 45sec after boot

[Unit]
Description=Mount Backup & Docker Volume @ reboot

[Timer]
OnBootSec=45sec
OnUnitActiveSec=1day
Unit=mount.volumes.service

[Install]
WantedBy=multi-user.target
  • Time to wait after booting before we run first time: OnBootSec

  • Time between running each consecutive time: OnUnitActiveSec

voila !

 

Part Four: Enable Service

Be aware, we havent finished yet!

Check that systemd can identify these files:

# systemctl list-unit-files | egrep mount.volumes

mount.volumes.service  disabled
mount.volumes.timer    disabled     

We can run the systemd service by hand:

# systemctl start mount.volumes.servicee

and see the ouput/results via journalct:

# journalctl -f

Part Five: Enable Timer

finally we need to start & enable (so that runs after reboot) the timer:

start

# systemctl start mount.volumes.timer

enable

# systemctl enable mount.volumes.timer

Created symlink /etc/systemd/system/multi-user.target.wants/mount.volumes.timer → /etc/systemd/system/mount.volumes.timer.

after that:

# systemctl list-timers | egrep mount.volume

Sat 2018-01-27 09:51:01 EET  23h left Fri 2018-01-26 09:51:01 EET  1min 16s ago mount.volumes.timer          mount.volumes.service

verify

# systemctl list-unit-files | egrep mount.volume

systemctl list-unit-files | egrep mount.volume
mount.volumes.service                                            disabled
mount.volumes.timer                                              enabled

To all the systemd haters, I KNOW, its one line on crontab !

Tag(s): systemd, timers
    Tag: systemd, timers
fluxbox keys »
  -  
« disabling pocket on firefox

Search

Admin area

  • Login

Categories

  • blog
  • wiki
  • pirsynd
  • midori
  • books
  • archlinux
  • movies
  • xfce
  • code
  • beer
  • planet_ellak
  • planet_Sysadmin
  • microblogging
  • UH572
  • KoboGlo
  • planet_fsfe

Archives

  • 2022
    • November
    • October
    • August
    • February
  • 2021
    • November
    • July
    • June
    • May
    • April
    • March
    • February
  • 2020
    • December
    • November
    • September
    • August
    • June
    • May
    • April
    • March
    • January
  • 2019
    • December
    • October
    • September
    • August
    • July
    • June
    • May
    • April
    • March
    • February
    • January
  • 2018
    • December
    • November
    • October
    • September
    • August
    • June
    • May
    • April
    • March
    • February
    • January
  • 2017
    • December
    • October
    • September
    • August
    • July
    • June
    • May
    • April
    • March
    • February
    • January
  • 2016
    • December
    • November
    • October
    • August
    • July
    • June
    • May
    • April
    • March
    • February
    • January
  • 2015
    • December
    • November
    • October
    • September
    • August
    • July
    • June
    • May
    • April
    • March
    • January
  • 2014
    • December
    • November
    • October
    • September
    • August
    • July
    • June
    • May
    • April
    • March
    • February
    • January
  • 2013
    • December
    • November
    • October
    • September
    • August
    • July
    • June
    • May
    • April
    • March
    • February
    • January
  • 2012
    • December
    • November
    • October
    • September
    • August
    • July
    • June
    • May
    • April
    • March
    • February
    • January
  • 2011
    • December
    • November
    • October
    • September
    • August
    • July
    • June
    • May
    • April
    • March
    • February
    • January
  • 2010
    • December
    • November
    • October
    • September
    • August
    • July
    • June
    • May
    • April
    • March
    • February
    • January
  • 2009
    • December
    • November
    • October
    • September
    • August
    • July
    • June
    • May
    • April
    • March
    • February
    • January
Ευάγγελος.Μπαλάσκας.gr

License GNU FDL 1.3 - CC BY-SA 3.0