[systemd-devel] Launching script that needs network before suspend

Lennart Poettering mzerqung at 0pointer.de
Mon Jan 31 12:01:30 UTC 2022


On So, 23.01.22 22:13, Tomáš Hnyk (tomashnyk at gmail.com) wrote:

> Hello,
> I have my computer hooked up to an AVR that runs my home cinema and ideally
> I would like the computer to turn off the AVR when I turn it off or suspend
> it. The only way to do this is over network and I wrote a simple script that
> does just that. Hooking it to shutdown was quite easy using network.target
> that is defined when shutting down.
>
>
> I am struggling to make it work with suspend though. When I look at the
> logs, terminating network seems to be the first thing that happens when
> suspend is invoked.

That shouldn't happen. Normally networking shouldn't be shut down
during suspend. If your network management solution does this
explicitly, I am puzzled, why it would do that.

> I tried putting the script to
> /usr/lib/systemd/system-sleep/ and it runs, but only after network si down,
> so it fails. Running the script with systemd-inhibit
> (ExecStart=/usr/bin/systemd-inhibit --what=sleep my_script) tells me that
> "Failed to inhibit: The operation inhibition has been requested for is
> already running".

Inhibitors are designed to be taken by long running apps, and not by
the stuff that is run when you are already suspending. i.e. it's too
late then: if you want to temporarily stall suspending to do your
stuff, it's already too late once these scripts are invoked, because
they are invoked once it was decided to actually go into suspend now.

> Is there a way to make this work with service files by specifying that the
> script needs to be run before network is shut down or would I need to run a
> daemon listening for PrepareForSleep as here:
> https://github.com/davidn/av/blob/master/av ?

Usually that's what you do, yes: you take an inhibitor lock while you
are running, and wait until you are informed about system suspend,
then you do your thing, and release the lock once you are done at
which point the suspend continues.

Lennart

--
Lennart Poettering, Berlin


More information about the systemd-devel mailing list