[systemd-devel] Starting a service at shutdown time, with requirements

Lennart Poettering lennart at poettering.net
Tue Mar 20 19:25:04 UTC 2018


On Di, 20.03.18 15:09, Colin Walters (walters at verbum.org) wrote:

> Another way I've thought about handling this is to basically invert things so that
> we have a "stub" unit that starts on bootup, and its ExecStop does
> the real work:

This is the correct and recommended way to do this.

> [Unit]
> Description=cgwalters-startstop
> ConditionPathExists=/run/ostree-booted
> DefaultDependencies=no
> RequiresMountsFor=/sysroot
> After=basic.target
> Before=multi-user.target
> Conflicts=final.target
> Before=final.target
> 
> [Service]
> Type=oneshot
> RemainAfterExit=yes
> ExecStart=/usr/bin/true

You can drop this line. On current systemd versions services without
ExecStart= are perfectly fine, as long as they have ExecStop=,
precisely to cover this usecase nicely.

> ExecStop=/usr/bin/sh -c 'sleep 5 && ls -ald /ostree/repo'
> 
> [Install]
> WantedBy=multi-user.target
> 
> Which seems to work but feels...a bit ugly?

I think it's relatively pretty, as it means you have to "start"
something explicitly so that it can run code at shutdown. I'd claim
this is a semantical benefit, not a malus.

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list