0

I am having some problems understanding the ideas behind systemd. What I am trying to do is configure a service which should normally run, and that depends on mariadb to work properly. Here is the (anonymized) .service file that I came up with:

[Unit]
Description=My Service
Requires=mariadb.service
After=mariadb.service

[Service]
ExecStart=/usr/bin/python2.7 -u /home/serviceuser/service.py
WorkingDirectory=/home/serviceuser
User=serviceuser

[Install]
WantedBy=multi-user.target

It worked well enough, but I noticed that sometimes the service would spontaneously stop. After some debugging I figured out that this coincided with mariadb security updates being installed automatically by ubuntu. Testing this, I found out that stopping mariadb will stop my service first (which makes sense to me) but starting mariadb back up again will not start my service.

I would expect that the WantedBy means that multi-user.target would really like My Service to run. So systemd should try to ensure that it runs whenever there is nothing that would prevent that. Things that would prevent it would be that the service or one of its dependencies was explicitly stopped. As soon as that preventing condition goes away, systemd should start the service again.

What is the mistake in this reasoning that leads to the service NOT being started again? How can I achieve the behavior I want with systemd?

2 Answers 2

0

mariadb is set as dependency of My Service so starting it will also launch mariadb if not running. Consequently stopping mariadb also stop My Service.

But if you only restart mariadb My Service will not be started automatically. You may like to enable My Service: systemctl enable My\ Service.service That will configure it to be started after booting up. But if I'm right stopping it shall keep it stopped until reboot or manual restart.

1
  • Thank you, but that only reiterates what I already found out. It does not actually answer either of the two questions.
    – Medo42
    Feb 27, 2018 at 22:55
0

You could add an WantedBy=mariadb.service

[Unit]
Description=My Service
Requires=mariadb.service
After=mariadb.service

[Service]
ExecStart=/usr/bin/python2.7 -u /home/serviceuser/service.py
WorkingDirectory=/home/serviceuser
User=serviceuser

[Install]
WantedBy=multi-user.target
WantedBy=mariadb.service

This will make systemd know MariaDB also wants to start your service(right now you only have the other way around).

Another thing is that you should enable your service again(using daemon-reload is not enough)

systemctl daemon-reload
systemctl enable <your_service>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .