Behaviour of systemd services on reboot
There is a great answer on the unix.stackexchange that I recommand you to read to fully understand how systemd works, and how to solve your issue. You can find it here : https://unix.stackexchange.com/questions/211924/effect-of-reboot-signal-on-systemd-service-state
Hope this can help you !
Related videos on Youtube
Pablo
I've studied computer science in Freiburg/Germany at the Albert-Ludwigs university and I'm currently working at mondas GmbH. My special field in the university was embedded systems and my diploma thesis was about a real-time micorkernel for a Gumstix board with an intel pxa255. Today I work mainly with Python. In the last company I used to work mainly with C, ruby (on rails) and python (django).
Updated on September 18, 2022Comments
-
Pablo over 1 year
On a Debian 8 I have deployed an application of mine that consists of several smaller apps and one of those smaller ones uses a HDF5 file. The problem with
HDF5
files is that you have to close them. If for example you program chrases or your program get killed, theHDF5
files get corrupted. Depending on the size of theHDF5
file, closing it can take several seconds.I have a tool
mondas_ctrl
which is a manager of my applications, their structure is not important here. When logged in (over ssh) I can executemondas_ctrl start
ormondas_ctrl stop
without a problem, specially the stop command knows how to deal with theHDF5
files (by adding delays inbetween).With the old SysV I was able to delay the halt process by changing the runlevel 6 on
/etc/inittab
wth a self written script that took care of the HDF5 files before doinginit 0
. Nowsystemd
is default and I had to change the way I think ofinit.d
files and services. So I wrote an user service that uses my manager to start and stop my apps:File:
~/.config/systemd/user/mondas.service
[Unit] Description=Mondas [Service] Type=oneshot RemainAfterExit=yes ExecStart=/home/mondas/anaconda/default/bin/mondas_ctrl start ExecRestart=/home/mondas/anaconda/default/bin/mondas_ctrl restart ExecStop=/home/mondas/anaconda/default/bin/mondas_ctrl stop KillMode=none [Install] WantedBy=default.target
At first I had a little bit of trouble with the stop command. After reading this thread I added the
Type
,RemainAfterExit
andKillMode
options. These helped a lot and now I can do this:$ systemctl --user start mondas $ systemctl --user stop mondas $ systemctl --user start mondas
as many times as I want, the stop command doesn't kill anything, my
mondas_ctrl
manager stops theHDF5
app, the file is closed and doesn't get corrupted.But when I do a system wide reboot (either by executing
reboot
orshutdown -r now
), theKillMode
flag seems to be ignored or the whole service seems to be skipped, the logs on screen pass by so quick, I cannot tell if my service is executed at all.So the question is, do "stop" services behave differently when you execute
systemctl stop
and when you executereboot/shutdown -r now
? If so, what do I have to do during reboot so that my app has enough time to close theHDF5
file?Thanks