[systemd-devel] How to start unit before everything else on shutdown

Lennart Poettering lennart at poettering.net
Sun Nov 30 15:03:50 PST 2014


On Wed, 26.11.14 13:06, Nikolaus Rath (Nikolaus at rath.org) wrote:

> Hello,

Heya,

> I'd like to start a unit on system shutdown and reboot, but do so
> immediately after the "systemctl reboot" command is executed, before any
> other units are terminated.

Well, "any" is difficult. I mean, if you have two units of this type,
what is supposed to happen then?

The best way to run things during shutdown is by introducing a service
with empty ExecStart= empty, but ExecStop= with the commands you want
to execute (also, please set RemainAfterExit=). Then, enable this unit
so that it is started normally at boot (which of course will have
little effect since your ExecStart= is empty, except that the service
will be marked running). Then, use After= and Before= in the service
to order it properly against the services you want yours to be run
before or after. Note however that in systemd the rule is that "start
order is reverse of stop order". Which means that if you declare
After=a.service in a service b.service, then this means that b.service
is stopped before a.service on shutdown!

Why the complexity of having a service that is started at boot-time
for this? This has to do with the simplicity of systemd's ordering
concept: when one unit is started and another one is stopped and they
are ordered against each other, then the stopping always takes place
before the starting, regardless which order is selected. WHile this
behaviour makes a lot of things simple and nice it also has the effect
that it is much nicer to order shutdown jobs against each other by
only stopping services there, and not mixing start and stopping in
it... 

But anyway, long story short: running something before "everything" or
after "everything" is not a concept we support in systemd. It has been
requested before, but we are not convinced that it is really a good
concept to add. Without this the best choice is to instead figure out
which services you precisely want to be started before and then write
a service that explicitly is ordered before or after them.

Hope this makes some sense,

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list