But the script is not triggered on sleep. I probably made some mistake in the unit file. Any suggestion?
Thanks for your help.
I wonder if using the approach used by the atd.sh script in /usr/lib/systemd/system-sleep/ may be helpful here?
#!/bin/sh
if systemctl --quiet is-enabled ftp_archivio; then
case $1/$2 in
pre/*)
systemctl stop ftp_archivio.service
;;
post/*)
systemctl restart ftp_archivio.service
;;
esac
fi
deano_ferrari, thanks for your hint. I tried your suggestions, now the script is triggered, but again, the log file shows that the eth is already down when killing the ftp connection.
> systemctl enable networkdown.service
The unit files have no [Install] section. They are not meant to be enabled
using systemctl.
Possible reasons for having this kind of units are:
1) A unit may be statically enabled by being symlinked from another unit's
.wants/ or .requires/ directory.
2) A unit's purpose may be to act as a helper for some other unit which has
a requirement dependency on it.
3) A unit may be started when needed via activation (socket, path, timer,
D-Bus, udev, scripted systemctl call, ...).
So, at least, the solution provided in the other forum is not fully documented (lacks of info about enabling the service). Unfortunately, this is well beyond my knowledge.
I ask you experts: what would be the way to get this service functioning (assuming that this unit file is not meant to be enabled using systemctl)?
I’m not sure what 'functioning" means here. If the question is, how to make sure this service is run as part of suspend - you either add suitable [Install] section and run “systemctl enable” or create necessary links manually. I.e. either
Another approach (I thought I’d posted about yesterday) is to cause the dispatcher script to get triggered by explicitly stopping or restarting NetworkManager.service ahead of the suspend by including a script in /usr/lib/systemd/system-sleep/ like this
#!/bin/sh
if systemctl --quiet is-enabled NetworkManager; then
case $1/$2 in
pre/*)
systemctl stop NetworkManager
;;
post/*)
;;
esac
fi
I tested with a simple dispatcher script in /etc/NetworkManager/dispatcher.d/, that does get executed, so hopefully this approach will work for you.
Thanks deano_ferrari for the idea. I developed it slightly: as you know, when the scripts inside /usr/lib/systemd/system-sleep/ are called with the “pre” argument, the eth/wlan connection is already down (at least in my opensuse 42.1 system).
I put the following script inside /usr/lib/systemd/system-sleep/, that, when the system goes to sleep (“pre” state), it forces restart of NM, waits 1 s, and launches the script that needs a network working to cleanly stop ftp connections.
> cat ftp_stop.sh
#!/bin/sh
if systemctl --quiet is-enabled NetworkManager; then
case $1/$2 in
pre/*)
systemctl restart NetworkManager
sleep 1
/home/michele/opt/tools/ftp_archivio/ftp_archivio_stop.sh
;;
post/*)
;;
esac
fi
I’m sure that the solution is not elegant, and it must be considered an hack, in lack of better alternatives.
I consider the problem solved, thank you all for the help.
Michele
Note that this makes NetworkManager unaware of suspend. At the very least it could cause excessive delays during AP scans after resume. Of course if you use fixed wired network it probably does not matter.
Yes at the moment the PC is connected via eth (but I would like that the solution is also usable on wlan).
Would you suggest adding a command, just after
in order to “make NetworkManager aware of suspend”?
If I brutally add a “systemctl stop NetworkManager”, on resume the NM need to be manually restarted (I think I could add it to the “post/*)” section).
Could you suggest a more correct way to proceed?
Thanks!
Michele
How about simply forcibly stopping network services before suspending?
I’m a bit unclear whether you just need to make sure your FTP connections are closed gracefully or if you want to forcibly close.
If the latter, you might simply forcibly stop network services before suspending.
To do this,
Copy systemd-suspend.service as follows
Then add the following ExecStop command to your copied file in the [Service] section
ExecStop=/usr/lib/systemd/network.service
Now, if you want to activate your new Unit file without re-booting (Might not be necessary. Required when you edit an existing Unit file that has already been loaded but I ran this just to be certain)
systemctl daemon-reload
You can now “systemctl start systemd-suspend.service” to suspend your machine.
I tested the above, and now after resuming “systemctl status network” reports that the network service did indeed stop when I suspended the machine.
The above avoids all the potential pitfalls of invoking the network manager instead of network services directly, and the possibility the methods you’ve used to this point to stop Network Manager themselves might have reported completed immediately while the services managed by Network Manager might still be changing state.
Another approach might be to simply script your shutdown… Your BASH script would serially shutdown your FTP or Network service and do anything else you might want before finally invoking the command to suspend.
That last approach of course doesn’t easily integrate with automatic system suspension but should work fine if a User is manually invoking suspend.