DEV Community

Adam.S
Adam.S

Posted on • Updated on • Originally published at bas-man.dev

Automation with Systemd & Code

Final part in a series of articles on implementing a notification system using Gmail and Line Bot

Today I will briefly share how the systemd configuration files are set and provide a link to the GitHub repo.

Systemd Service

I am running my code under Debian. In order to learn something new. I chose to use systemd to provide scheduling for the script execution.

These files should be placed under /etc/systemd/system/

Here is the script: gnotifier.service

[Unit]
Description=Monitor Gmail Notifier
Wants=gnotifier.timer

[Service]
Type=oneshot
ExecStart=/usr/bin/python3 /home/ACCOUNT/gmail/gmail.py
WorkingDirectory=/home/ACCOUNT/gmail


[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

This is fairly straight forward. The filename itself provides the service name gnotifier. We also specify that this script executes once and terminates: oneshot.
To have the service execute multiple times we provide the Wants=gnotifier.timer; this says that we need to read gnotifier.timer to get schedule information.

Systemd Timers

gnotifier.timer

[Unit]
Description=Triggers gnotifier service
Requires=gnotifier.service

[Timer]
Unit=gnotifier.service
# KidzDuo arriving
OnCalendar=Mon,Tue,Thu *-*-* 14:20,25,30,35,40,45,50,55:00
# Swimming
OnCalendar=Wed *-*-* 15:50,55:00
OnCalendar=Wed *-*-* 16:0,5,10,15,20:00
OnCalendar=Wed *-*-* 18:0,5,10,15,20,25:00
# KidzDuo Leaving
OnCalendar=Mon,Tue,Thu *-*-* 18:0,5,10,15,20,25,30,35:00
OnCalendar=Mon,Tue,Thu *-*-* 19:0,5,10,15,20,25:00
AccuracySec=1s

[Install]
WantedBy=timers.target
Enter fullscreen mode Exit fullscreen mode

There are many ways to configure the timers. Here I have chosen OnCalendar
Let's take a look at one example:
OnCalendar=Mon,Tue,Thu *-*-* 14:20,25,30,35,40,45,50,55:00

Days of the week: Mon,Tue,Thu

Hour: 14 (2pm)

Minutes: 20,25,30,35,40,45,50,55 Every 5 minutes starting at 20 minutes.

Seconds: 00

Enable the service

You will need to issue the following commands

systemctl enable gnotifier
systemctl start gnotifier
Enter fullscreen mode Exit fullscreen mode

You can check your systemd timers using

systemctl list-timers
Enter fullscreen mode Exit fullscreen mode

Some references

OpenSource Article

ArchLinux Wiki

GitHub Repo

You can find the code for this project here.

Top comments (0)