How can I launch a systemd service at startup before another systemd service starts?

 

There are 3 main properties for managing dependencies. I'll try to briefly explain the difference but you can find more details at [Unit] Section Options | freedesktop.org

  1. After=

    This option only sets the order of the units, it doesn't guarantee that the service has finished starting up.

  2. Wants=

    This option allows your unit to start only after another unit has finished starting up. (Doesn't matter if it started successfully or not)

  3. Requires=

    Just like Wants=, however, this will make your unit start only after the dependencies have successfully started.

You can also use the inverse of each of those options.

  1. After= is inversed by Before=
  2. Wants= is inversed by WantedBy=
  3. Requires= is inversed by RequiredBy=

To fix your problem, you need to change your mount service to:

[Unit]
Description=MountSmokeScreen
After=network.target
Before=sonarr.service radarr.service

[Service]
Type=oneshot
ExecStart=/home/samsepioldoloresh4ze/bin/check.mount
TimeoutStopSec=20
KillMode=process
Restart=on-failure

[Install]
WantedBy=multi-user.target sonarr.service radarr.service

Or you can add Wants=mountgdrive.service to the sonarr.service and radarr.service units.

You can do that without modifying the default files by running the following (You need to do the same for radarr.service):

systemctl edit sonarr.service

And insert the following:

[Unit]
Wants=mountgdrive.service

Note: You can replace Wants= with Requires or WantedBy= with RequiredBy= if you don't want the two services to start at all if mountgdrive.service fails (Though Wants= is usually enough and even recommended in the docs).

EDIT: The WantedBy and RequiredBy options can only be used under the [Install] section. (Thanks @Yankee)

 

[Source:  https://askubuntu.com/questions/1024916/how-can-i-launch-a-systemd-service-at-startup-before-another-systemd-service-sta ]


Keine Kommentare:

Kommentar veröffentlichen

Datei in einer windows.wim image ändern

 Um Dateien in einer Datei.wim zu ändern, und zwar nicht im 1. Index sondern 2. öffnet man Powershell (ich habs jetzt im admin Modus gestart...