0

I have a server with an encrypted filesystem which must be manually brought online after a reboot. Some services require that encrypted filesystem to be mounted in order to read configuration/data files, etc.

Is there a way to tell systemd that whenever service X starts (even manually), services Y and X should be started automatically?

I know I can script this with a "start all my stuff" script that starts the encrypted filesystem service (which isn't really a service; just a complicated mount command) and then starts everything else I need, but I was wondering if this is something systemd can handle on its own.

Christopher Schultz
  • 1,105
  • 2
  • 12
  • 22

1 Answers1

1

So if we name your service X and the other service which should be started automatically (the filesystem) service Y, the following should do the job:

shell# vim /usr/lib/systemd/serviceX.service

#Inside the unit file of service x add the following in the [unit] section:
[Unit]
Requires=serviceY.service

#For multiple services
[Unit]
Requires=serviceY.services serviceZ.services

# Don't forget to relaod systemd
shell# systemctl daemon-reload

Now if you start service x, service y (the file system) should be started autaomatically. Furthermore if there is an error with service y and it can't start service x won't start either. Sidenote: There are other keywords like "after" or "wants", which are less strict than "Requires". But in your example "Requires" should be the right choice.

Lorem ipsum
  • 892
  • 5
  • 15
  • So let's assume that X is a service which requires the filesystem (serviceY.service). The filesystem will fail to start on initial boot, so service X will also not be started. The question is this: if I manually start serviceY.service (the filesystem), will systemd automatically start serviceX.service for me, or will I have to manually start that as well? – Christopher Schultz Feb 10 '20 at 17:59
  • In this situation i would edit the unit file of serviceY.service (the filesystem) and add the following: Wants=serviceX.service (similiar to the answer). The keyword wants makes serviceX to start if serviceY is started but if the startup of serviceX fails serviceY still starts up. – Lorem ipsum Feb 10 '20 at 19:38
  • I think you have this backwards: it's serviceX that needs serviceY. serviceY fails and I indeed want serviceX to *not* start. But when serviceY becomes available, I want serviceX to start automatically. Is this possible? – Christopher Schultz Feb 10 '20 at 19:47
  • Yes, the configurations provided will do this. For serviceX unit-file you specify requires service Y and additionally for serviceY unit-file you specify wants serviceX. Now whenever serviceY starts servicex gets started. And serviceX can only be started if serviceY is running. However, if servicex can't startup you still can start serviceY. This is the setup you will realize with the configurations above. – Lorem ipsum Feb 10 '20 at 20:03