18

I've written about half a dozen .service files for the different parts that make up the system I'm working on. It's useful to be able to start the whole system piecemeal but I'd also like to have a single unit that starts the whole system in one call to systemctl.

What's the best way to do this?

2 Answers 2

18

You want a target-type unit, with all the service units listed as Wants= dependencies. Then you start it using systemctl start unitname.target. (Make sure not to use systemctl isolate here; that will shut down everything except what's in your services' dependency tree, which you presumably don't want.)

7
  • 2
    Alternatively, you could use the WantedBy key in the [Install] section and note an already-existing target. For example, if you want your services to always start up before other users can log in, use multi-user.target. Sep 8, 2015 at 19:40
  • 1
    Yes, the target type with Wants= listing all the services seems sufficitient for starting, however I can't seem to find any way to make stop and restart work using a target type. Is there a type that allows that?
    – Magnus
    Sep 9, 2015 at 6:51
  • 3
    Sam, Magnus: It looks like stopping the wanted services from a .target can be done using StopWhenUnneeded=yes in each of the .service files. superuser.com/questions/470153/…
    – Jon Nordby
    Dec 13, 2016 at 16:09
  • 2
    In my particular case I can make use of the systemd tools handling globbing :) So, for starting I use a .target and for stopping I use systemctl stop foo.\* (since all the services involved have service files starting wit foo.).
    – Magnus
    Mar 31, 2017 at 8:57
  • 1
    Setting PartOf=some.target on service unit will make it restart when restarting the target Apr 21, 2022 at 16:52
15

Have you tried using PartOf on the sub-units of the target group in addition to Wants in the target? Ex: PartOf=foo.target in foo-sub1.service and Wants=foo-sub1.service foo-sub2.service in foo.target. I just tried this and was able to start and stop all my sub-units just by starting and stopping the target.

PartOf=

Configures dependencies similar to Requires=, but limited to stopping and restarting of units. When systemd stops or restarts the units listed here, the action is propagated to this unit. Note that this is a one-way dependency — changes to this unit do not affect the listed units.

When PartOf=b.service is used on a.service, this dependency will show as ConsistsOf=a.service in property listing of b.service. ConsistsOf= dependency cannot be specified directly.

1
  • 3
    This provides the other half of the answer give by Tom Hunt--the ability to stop an entire group of services. Apr 30, 2019 at 9:52

You must log in to answer this question.

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