systemd script executed "too late"

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.

I’m doubtful about the unit file and how to trigger it: indeed in the http://askubuntu.com/questions/681109/networkmanager-dispatcher-d-pre-down-d-is-not-executed-on-shutdown-anymore the unit file lacks of the [install] section.
If I try to start it without the [install] section, this error occurs:

> 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)?

Yours sincerely,
Michele

You try to enable it, not start. “enable” simply creates links from [Install] section so of course it fails if no [Install] section is present.

arvidjaar, I already know this, as I wrote

this unit file is not meant to be enabled using systemctl
Thanks anyway.
My question was: what would be the way to get this service functioning?

I hope that the answer to this simple task (i.e. I just need to run a script before network is stopped via systemctl suspend) would not switch to windows (it’s just a joke, already made for a similar problem: NetworkManager-dispacther: pre-down actions no longer working / Networking, Server, and Protection / Arch Linux Forums) or try to find alternatives to the NetworkManager networking system.
Yours sincerely
Michele

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

[Install]
WantedBy=sleep.target

or

mkdir -p /etc/systemd/system/sleep.target.wants
ln -s .../your-service.service /etc/systemd/system/sleep.target.wants

If you want to distinguish between sleep method (i.e. suspend and hibernate), use suspend.target, respectively hibernate.target.

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.

#!/bin/bash
echo "$(date) stopped" >> /tmp/ftp_archivio_stop.log
sleep 5

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


    pre/*)
      systemctl restart NetworkManager
      sleep 1
      /home/michele/opt/tools/ftp_archivio/ftp_archivio_stop.sh

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

I didn’t find that to be the case. It just starts back up as it was before the suspend.

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

cp /usr/lib/systemd/system/systemd-suspend.service /etc/systemd/system/

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.

HTH,
TSU

Hi just one suggestion, instead of

ps | grep

You can try

pgrep
if pgrep lftp ....; then 
   ...
fi

Also

pgrep --help

for more options.

Now if you are running the ftp process yourself then you can just save its pid

ftp blah...blah...blah & pid=$!
echo "$pid"
kill "$pid"