8

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?

9

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.)

  • 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. – saiarcot895 Sep 8 '15 at 19:40
  • 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 '15 at 6:51
  • Magnus, did you ever find a good solution for this? – Sam Hendley Oct 20 '15 at 22:36
  • 2
    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/… – jonnor Dec 13 '16 at 16:09
  • 1
    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 '17 at 8:57
1

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.

  • Welcome to U&L! If your post doesn't provide an answer to the question then it should be posted in Comment section bellow the question. – Yurij Goncharuk Apr 30 at 9:06
  • This provides the other half of the answer give by Tom Hunt--the ability to stop an entire group of services. – Benny Jobigan Apr 30 at 9:52

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.