[systemd-devel] Help with running a script at shutdown and make it run first before other services
David Elie-Dit-Cosaque
deliedit at redhat.com
Wed Dec 11 23:37:33 UTC 2024
I finally found a way to make it work *once* but unless there is a wildcard
mechanism in systemd or similar I am not sure how to make it work across
several reboots.
So I am trying to run this script in a kubernetes node which manages
containers using crio and systemd (see
https://kubernetes.io/docs/tasks/administer-cluster/kubeadm/configure-cgroup-driver/).
Basically every container is tracked with a crio-*.scope. Scope names are
random. The crio-*.scope are started by slices but in my tests the parent
slice does not change the shutdown order of its childrens.
I'd like to run my script before any of these scopes (containers) are
stopped.
So I did a test listing every crio-*.scope present in the cluster in the
After= directive and it worked ! My script was reordered to run before any
scopes/containers could be stopped by systemd.
[Unit]
After=crio-021c01dd1629e3571606b9398bf30734ffd1aa0dd621c8dde779b3017fedcae7.scope
crio-02613e7b303f3a49c129d859605f9a1c1c1fde6f7f0d2a764c4604fb4e436df5.scope
... + many lines
crio-dc592c6ac726c20ce4a46cebe0807574579de30b97bc75a9be057e5df0eb4e19.scope
crio-dda82b33e6f48b47cff898d62aefdf556c9f0d8b958babe608ef4989e6400340.scope
[Service]
Type=oneshot
RemainAfterExit=true
ExecStop=/usr/local/bin/myscript.sh
[Install]
WantedBy=multi-user.target
Is there a way to use wildcards in After= like After=*.scope or something
equivalent. I cannot hardcode the .scope names because they are random.
Thanks!
David
On Mon, Dec 9, 2024 at 8:27 AM David Elie-Dit-Cosaque <deliedit at redhat.com>
wrote:
> Hi Dan and Andrei, thanks a lot for the explanation and code snippets, I
> try it out.
>
> Thanks,
> David.
>
> On Sun, Dec 8, 2024 at 10:13 AM Dan Nicholson <dbn at endlessos.org> wrote:
>
>> On Fri, Dec 6, 2024 at 3:16 PM David Elie-Dit-Cosaque
>> <deliedit at redhat.com> wrote:
>> >
>> > Hi All,
>> >
>> > I am trying to create a systemd service that would execute a script at
>> shutdown before any other services start receiving shutdown notifications.
>> I came up with this:
>> >
>> > [Service]
>> > Type=oneshot
>> > RemainAfterExit=true
>> > ExecStart=/usr/bin/true
>> > ExecStop=/usr/local/bin/myscript.sh
>> > [Install]
>> > WantedBy=multi-user.target
>> >
>> > The script does run after shutdown is initiated but since it relies on
>> other services which are also shutting down, it fails. Is there a way to
>> order the shutdown so that myscript.sh is completed first before
>> continuing with the shutdown?
>>
>> When stopping units, systemd reverses the ordering. For example, if
>> you have a service that has After=ssh.service, then the service is
>> started after ssh.service and stopped before it.
>>
>> So, you just need to add After for any services or targets that you
>> care about even if you don't have ExecStart. Here's an example of a
>> service that runs on shutdown that does nothing when starting -
>>
>> https://github.com/ostreedev/ostree/blob/main/src/boot/ostree-finalize-staged.service
>> .
>>
>> ---
>> Dan
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/systemd-devel/attachments/20241211/f1e2c0c7/attachment.htm>
More information about the systemd-devel
mailing list