0

I'm trying to get Systemd to run a script that sends out an email before the computer shuts down. I understand Systemd terminates services in the reverse order they are started, so if you put After=network.target, your service is shut down before network.target.

However, no matter how many damn depenencies I put on the After= and Wants= lines, the network connection goes down before my email script runs. Occasionally it will send an email, but the vast majority of times it doesn't. This tells me there is a race condition and Systemd is not waiting for my script to finish before it shuts down the networking service.

My unit file looks like this:

[Unit]
Description=Send email before shutdown
Wants=network.target nss-lookup.target network-online.target networking.service NetworkManager.service network-manager.service
After=network.target nss-lookup.target network-online.target networking.service NetworkManager.service network-manager.service

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/home/user1/send_email.sh

[Install]
WantedBy=multi-user.target

The journal says:

Jun 20 13:13:14 caesar systemd[1]: Stopped Availability of block devices.
Jun 20 13:13:14 caesar NetworkManager[699]: <info>  [1559301194.4305] device (wlx24050fd83726): state change: deactivating -> discon
Jun 20 13:13:14 caesar NetworkManager[699]: <info>  [1559301194.4629] dhcp4 (wlx24050fd83726): canceled DHCP transaction, DHCP clien
Jun 20 13:13:14 caesar NetworkManager[699]: <info>  [1559301194.4629] dhcp4 (wlx24050fd83726): state changed bound -> done
Jun 20 13:13:14 caesar NetworkManager[699]: <info>  [1559301194.4648] manager: NetworkManager state is now DISCONNECTED
Jun 20 13:13:14 caesar NetworkManager[699]: <warn>  [1559301194.4661] sup-iface[0xb4fd40,wlx24050fd83726]: connection disconnected (
Jun 20 13:13:14 caesar NetworkManager[699]: <info>  [1559301194.4662] device (wlx24050fd83726): supplicant interface state: complete
Jun 20 13:13:14 caesar send_email.sh[21782]: s-nail:  Certificate depth 2
Jun 20 13:13:14 caesar send_email.sh[21782]: s-nail:   subject = /C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert 

....

Jun 20 13:13:56 caesar send_email.sh[21782]: s-nail: could not initiate SSL/TLS connection: error:00000000:lib(0):func

I don't think it should be this difficult. I'd appreciate any pointers as to what I'm doing wrong.

  • Adding more dependencies not always helps, since if you end up creating a dependency loop, systemd will break one of the dependencies to break the loop, and it might end up being exactly the one you needed... Try to remove the ,Wants= and keeping only After=network.target since that's the only one you need – filbranden Jun 22 at 16:45
  • Also, do you have a line Stopped Send email before shutdown in your journal logs? That might help check when it's running the script... Does send_email.sh run anything in background? Or does it block until the email is sent? Does this work if you run systemctl stop <your-service-name> on the command line when the system is up and running? Can you check if the service is running right before shutdown, with systemctl status? – filbranden Jun 22 at 16:47
  • The systemd-analyze tool can be useful to debug this kind of issue. See systemd-analyze dump for example, or systemd-analyze plot. You can also use it to increase logging. Most of systemd-analyze is tailored towards startup (time to start, etc.) but it can be helpful checking shutdown problems too... – filbranden Jun 22 at 16:54
0

You should also specify a "Requires=network.target" line. More info at https://fedoramagazine.org/systemd-unit-dependencies-and-order/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.