When you develop a service or
application, you want it startup along with the OS, or you want to schedule it
running in with specific timing. This is how you can do it with systemd daemon,
a fabulous service running in Unix-like system to manage and startup all other
services.
As it was verified and used for
Linux, so we don’t need to worry about it.
It would be a first daemon service
startup during system booting time and the last daemon terminated during
shutdown (this is also the diff between reboot and shutdown command), so
somehow it will help us better manage and clean the thing.
Beside all the functions can do like
cron.
Here some advantages systemd over
cron:
- Newer than cron 😉
- All systemd timer events are logged in systemd journal ($ journalctl)
- Allow define dependencies, schedule to startup, schedule before or after some specific services
- Can start or stop service and all invoked dependencies just by one command
- Service can be restart or startup base on many different events like user, boot, hardware state changes or an exit error return
- Just config by 1 configuration file for each service, no scripts to maintain at all
- Systemd time events (calendar) are more accurate than cron (seems 1s precision) as many other developers said. 😝
- From the CPU usage viewpoint systemd timer wakes the CPU on the elapsed time but cron does that more often.
- Timer events can be scheduled based on finish times of executions, some delays can be set between executions.
- And more …..
How to start up your application
I prepared the examples for you,
suppose we have a Raspberry pi board with Linux on there (or you also can do
with any computer with Linux running) and put all the files in
/home/pi/Documents/
In order to enable and start using
$
sudo cp pi-mon.service pi-mon-http.service pi-mon.timer /etc/systemd/system
$ sudo systemctl daemon-reload
$ sudo systemctl enable pi-mon-http.service
$ sudo systemctl start pi-mon-http.service
$ sudo systemctl status pi-mon-http.service # Optional - Check status of service
$ sudo systemctl daemon-reload
$ sudo systemctl enable pi-mon-http.service
$ sudo systemctl start pi-mon-http.service
$ sudo systemctl status pi-mon-http.service # Optional - Check status of service
OK! Let verify them:
pi-mon will be scheduled to run
every 1 min and log the resources info into /home/pi/Documents/[current-date].
In order to check it, just read this file. After 1 min, you can check this file
again to verify it is run by the schedule.
.e.g: your current date is
2020-04-19
$
cat /home/pi/Documents/2020-04-19
pi-mon-http will be run right after
pi-mon. In order to verify this guy, open the browser and browse to the
Raspberry pi IP address with port 5000 and some postfix like (cpu, cputemp,
gputemp, ..etc..), you can view these postfixes in the source file
pi-mon-http.py
.e.g: your Raspberry pi IP
address is 192.168.1.12. Then you open a browser on any computer in the same
local network with pi and browse to "192.168.1.12:5000/cputemp", you
will see the current temperature of cpu core on Raspberry pi. And now if you
feel it's too hot, you're free to pour some water or coffee on there. 😜
Some notes:
[pi-mon-http.service]
Requires=pi-mon.service pi-mon.timer # dependencies
After=pi-mon.service pi-mon.timer # will be run after these services
ExecStart=/usr/bin/python3 /home/pi/Documents/pi-mon-http.py # execute cmd
Restart=always # always restart if crash or exit does not matter the error
User=pi # run under user pi
Similar for [pi-mon-http.service]
[pi-mon-http.timer]
PartOf=pi-mon-http.service # This service stopped when we stop the parrent service
pi-mon-http.service
OnCalendar=*:00/1 # restart every 1 mins
In order to stop services (All
dependencies will be stopped too):
$
sudo systemctl stop pi-mon-http.service
to disable service (not startup
during booting anymore):
$
sudo systemctl disable pi-mon-http.service
p/s: The source files are also
something very interesting, if you guys got time, you can have view on them. I
will not explain them here, because it's not the purpose of this post.
Have a nice day. Stay health my
friends.