systemd.timer

short guide for acme-client

As an alternative to the “well known crontab” systemd.timer presents itself as a well controllable and highly extensible solution. I would like to give a short explanation and an example of how systemd.timer can be used. In the following case a timer is used to synchronize a GitLab repository.

The first thing to do is to create the shell script to be executed

> mkdir -p /opt/scripts
> edit /opt/scripts/hugo-ci.sh

As content of the script the GitLab repository is pulled, as well as the rights of the web server directory are adapted

#!/bin/sh
set -xe
cd /var/www/html
git pull
chown -R www-data:www-data /var/www/html
exit

Important for the successful execution of the script is to make it executable by means of

> chmod +x /opt/scripts/hugo-ci.sh

Two systemd units are to be created for the implementation. Start with the actual service under /usr/lib/systemd/system/hugo-ci.service

[Unit]
Description=CI Pipeline for goHugo

[Service]
Type=simple
ExecStart=/opt/scripts/hugo-ci.sh

[Install]
WantedBy=multi-user.target

Afterwards the timer is created under /usr/lib/systemd/system/hugo-ci.timer. The previously created service is executed under hourly, this is controlled under OnCalendar.

[Unit]
Description=Pull goHugo CI every hour

[Timer]
OnCalendar=*-*-* *:00:00
Unit=hugo-ci.service

[Install]
WantedBy=multi-user.target

After the service to run the script and the timer of the same name have been created, the timer is enabled and started to run the service.

> systemctl enable hugo-ci.timer
> systemctl start hugo-ci.timer

Checking whether the timer is enabled and active is done as follows

> systemctl is-enabled hugo-ci.timer
> systemctl is-active hugo-ci.timer

To test the function of the service, it can be executed directly

> systemctl start hugo-ci

If changes are subsequently made to the timer, a reload of the systemd daemon is required

> systemctl daemon-reload

To display how much time is left until the next triggering, use the following command

> systemctl list-timers hugo-ci.timer
--
NEXT                          LEFT       LAST                          PASSED    UNIT          ACTIVATES
Sat 2019-05-25 23:00:00 CEST  12min left Sat 2019-05-25 22:00:15 CEST  47min ago hugo-ci.timer hugo-ci.service
---

The following is a not completed list of possible timers

Minimal form                   Normalized form

Sat,Thu,Mon-Wed,Sat-Sun    ==> Mon-Thu,Sat,Sun *-*-* 00:00:00
Mon,Sun 12-*-* 2,1:23      ==> Mon,Sun 2012-*-* 01,02:23:00
Wed *-1                    ==> Wed *-*-01 00:00:00
Wed-Wed,Wed *-1            ==> Wed *-*-01 00:00:00
Wed, 17:48                 ==> Wed *-*-* 17:48:00
Wed-Sat,Tue 12-10-15 1:2:3 ==> Tue-Sat 2012-10-15 01:02:03
*-*-7 0:0:0                ==> *-*-07 00:00:00
10-15                      ==> *-10-15 00:00:00
monday *-12-* 17:00        ==> Mon *-12-* 17:00:00
Mon,Fri *-*-3,1,2 *:30:45  ==> Mon,Fri *-*-01,02,03 *:30:45
12,14,13,12:20,10,30       ==> *-*-* 12,13,14:10,20,30:00
mon,fri *-1/2-1,3 *:30:45  ==> Mon,Fri *-01/2-01,03 *:30:45
03-05 08:05:40             ==> *-03-05 08:05:40
08:05:40                   ==> *-*-* 08:05:40
05:40                      ==> *-*-* 05:40:00
Sat,Sun 12-05 08:05:40     ==> Sat,Sun *-12-05 08:05:40
Sat,Sun 08:05:40           ==> Sat,Sun *-*-* 08:05:40
2003-03-05 05:40           ==> 2003-03-05 05:40:00
2003-03-05                 ==> 2003-03-05 00:00:00
03-05                      ==> *-03-05 00:00:00
hourly                     ==> *-*-* *:00:00
daily                      ==> *-*-* 00:00:00
monthly                    ==> *-*-01 00:00:00
weekly                     ==> Mon *-*-* 00:00:00
*:20/15                    ==> *-*-* *:20/15:00