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 <d...@endlessos.org> wrote: > On Fri, Dec 6, 2024 at 3:16 PM David Elie-Dit-Cosaque > <delie...@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 > >