[systemd-devel] Need help: Program not run when rebooting

Lennart Poettering lennart at poettering.net
Thu Mar 11 15:50:43 UTC 2021


On Do, 11.03.21 15:53, Ulrich Windl (Ulrich.Windl at rz.uni-regensburg.de) wrote:

> Hi!
>
> I tried to write a simple test unit that logs a message when the system is
> going down (for reboot or halt), but it does not work:
> ---
> [Unit]
> Description=Test Stop log entries
> Documentation=man:logger(1)
> DefaultDependencies=no
> After=local-fs.target
> Before=reboot.target halt.target shutdown.target poweroff.target kexec.target
> Wants=local-fs.target

This is contradictory: you want to be pulled in at shutdown, but are
pulling in a target (and its dependencies) that are supposed to go away
at shutdown.

During shutdown your service will be started, but local-fs.target will
go down. Now, After=/Before= declare the ordering when both the source
and the target unit are started, or when both the source and the
target unit are stopped. When one unit is started and another one
stopped, then After=/Before= only have the effect that the unit to
stop is stopped first, and the unit to start is started second,
regardless if you actually use After= or Before=. See docs for more on
this.

So this means that local-fs.target has to go down first, then your
service is started. Is this really what you want?

Typically, if you want to run stuff durin shutdown you write a service
that only has ExecStop= (and no ExecStart=) and pull that in at boot
already. Doing this won't run any processes at boot, but just declare
that this service is among those now managed by systemd to make sure
we'll run ExecStop= eventually.


> Mar 11 15:17:33 jeos1 systemd[1]: Stopped target Local File Systems.
> Mar 11 15:17:33 jeos1 systemd[1]: Started Test Stop log entries.
> Mar 11 15:17:33 jeos1 systemd[1]: Unmounting /usr/local...
>
> So I want to run my program much earlier; how to do that?

If you want that your service's ExecStop= line runs *before* the local
mount are shutdown, use something like this:

<snip>
[Service]
ExecStop=…
RemainAfterExit=yes
Type=oneshot

[Install]
WantedBy=multi-user.target
</snip>

i.e. make it a regular service, with no special deps. The normal dep
should be totally sufficient. But only declare a ExecStop= line.

Lennart

--
Lennart Poettering, Berlin


More information about the systemd-devel mailing list