[systemd-devel] Launching script that needs network before suspend
arvidjaar at gmail.com
Mon Jan 24 17:56:59 UTC 2022
On 24.01.2022 17:37, Tomáš Hnyk wrote:
> On Monday 24. January 2022, 13:50:48 (+01:00), Andrei Borzenkov wrote:
I posted it in response to the list and you sent personal reply. Please use
reply to all.
>> On Mon, Jan 24, 2022 at 1:14 AM Tomáš Hnyk <tomashnyk at gmail.com> wrote:
>>> 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. 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".
>> What network management program are you using?
> Network Manager. I tried putting "systemctl start NetworkManager.service" into the /usr/lib/systemd/system-sleep/ hook but even though the log says network is reestablished, the script still fails.
NetworkManager has own hooks. Unfortunately there is no hook that runs on
suspend/resume - only on interface up/down. You may want to post to NM list
asking whether something like this could be implemented (but it will take
a long time).
Is performing your action on interface down acceptable? If interface stays
up all the time except during suspend or shutdown it looks like workaround.
>>> 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 ?
>> Yes, this is probably the only generic solution.
> I tried the script I linked and it does not work either (there seems to be a race condition that I am losing). What I am not clear about inhibiting is this: when PrepareForSleep is called and I have a script hooked into it that inhibits sleep, does it inhibit sleep from finishing or from running everything that is triggered by it? To me it seems it will just delay going to sleep, but shutting the network down is not inhibited.
NetworkManage takes sleep lock and subscribes to PrepareForSleep()
signal, so your daemon races with NetworkManager. There is no way
to change it - various programs subscribed to PrepareForSleep() are
not aware of each other at all.
These programs run outside of systemd unit management so you cannot
use "normal" systemd dependencies to order them.
More information about the systemd-devel