3

I have a service that depends on another:

[Unit]
After=main.service
Requires=main.service

[Service]
ExecStart=/usr/local/bin/dependent
Restart=always

The kool things:

  • Running systemctl stop main causes this dependent service to also stop
  • Running systemctl restart main causes dependent service to also get started

But, running systemctl main start does not cause this dependent service to get started.

How do I do it?

4

The After= and Before= parameters are strictly for ordering the startup (and maybe the shutdown) of two services if both are asked to be started. Having a service listed there doesn't mean that those services will also be started up.

If you want to control whether or not another service gets started, you need to use Requires=, Wants=, or Conflicts= (which go in the [Unit] section) or WantedBy= (which goes into the [Install] section).

In your case, you want to start a service when something it's dependent on is started (which doesn't happen automatically, as there is no reason to do so). You'll want to add the following to the file and then run systemctl enable dependent:

[Install]
WantedBy=main.service

This says that when main is started, to also start this service, and that if this service happens to fail, then don't stop main.

  • It's worth noting that by itself, adding the WantedBy= as suggested won't get the other service to start with systemctl start main.service. It's not until the main.service is enabled that happens. See the associated docs for WantedBy= here... – Mark Edington Nov 20 '17 at 19:44
  • Sorry, forgot the link: man7.org/linux/man-pages/man5/systemd.unit.5.html Also note that still doesn't seem to work for my test despite creating a symlink. Using "Wants=" instead in the Unit section works the best but requires a potentially unwanted reference from main.service back to the dependent service. – Mark Edington Nov 20 '17 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.