[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
Sat Dec 7 00:04:49 UTC 2024


Thanks for your reply, I tried this before but could not make it work as I
wanted to. If I remember well, the service kept starting during boot
instead of shutdown. After struggling with it I found this page:
https://unix.stackexchange.com/questions/39226/how-to-run-a-script-with-systemd-right-before-shutdown
which seems to work, except I could not find any ways to control the
ordering/dependencies. I'll try again as you suggest,

Thanks,
David.

On Fri, Dec 6, 2024 at 5:45 PM Nils Kattenbeck <nilskemail at gmail.com> wrote:

> You should probably just use an ExecStart= (not with the stop you are
> currently using), have it WantedBy=shutdown.target, and Before= any service
> which you depend on to still be available
>
> On Sat, Dec 7, 2024, 00:30 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?
>>
>> Thanks!
>> David.
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/systemd-devel/attachments/20241206/1aeffb1d/attachment.htm>


More information about the systemd-devel mailing list