Question

systemd dependencies . Running a script before shutdown

The requirement seems quite easy (before shutting down/rebooting remove a droplet tag) but getting it working just isn’t happening.

I have the following script which works for quickly adding/removing a droplet to the loadbalancer tag /root/SCRIPTS/loadbalancer.sh

#!/bin/bash
API=xyxyxyxyxyxyxyxyxyxyxyx
SID=`/snap/bin/doctl -t $API compute droplet list --format ID,Name | grep $HOSTNAME`
SID="$(echo $SID | cut -d ' ' -f1)"

function add {
        /snap/bin/doctl -t $API compute droplet tag $SID --tag-name=WEB-LBD
}

function remove {
        /snap/bin/doctl -t $API compute droplet untag $SID --tag-name=WEB-LBD
}

case "$1" in
add)
        echo "Adding Server to LoadBalancer..."
        add
        ;;
remove)
        echo "Removing Server from LoadBalancer..."
        remove
        ;;
debug)
        echo "Output:"
        echo "$SID"
        ;;
*)
        echo "(add|remove|debug)"
        ;;
esac

This works great (and I’ve made another script calling this one before and after running updates). However what I was hoping to do, is to remove the server from the loadbalancer automatically before the server is reboot/shutdown

I’ve create /etc/systemd/system/loadbalancer-remove.service

[Unit]
Description=Remove Server from LoadBalancer on shutdown
After=network.target networking.service network-online.target nss-lookup.target systemd-resolved
Requires=network.target networking.service network-online.target nss-lookup.target systemd-resolved

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/root/SCRIPTS/loadbalancer.sh remove

[Install]
WantedBy=multi-user.target

The service is active but seems to run the exit after the network has already gone (or something it needs). According to some systemd programmer, all that’s needed to ensure this exits before the network is shutdown is “network.target” but the droplet hangs on reboot. I believe this is because doctl obviously needs the network up to communicate and the network is going down before this is stopped but it’s difficult to know this as I can’t see any output as it’s shutting down. I’ve tried various Wants, Requires & After but it’s the same result. I found a different way of doing it that uses halt.target reboot.target shutdown.target before starting this service, but that gave me the same issue : hang on reboot.

Just wondering if anyone has solved anything similar? it’s not specifically a doctl issue but I dont know if doctl taking a few seconds is allowing the network to be shutdown before it’s finished.

Any thoughts at all appreciated, I’ve been googling and trying stuff for 4 hours.


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

This comment has been deleted

Doing some digging indicates that

After=networking.service

Should do the trick. If you remove all other items in After do you still encounter problems?

While this configuration should be achievable, it would not handle unplanned downtime such as crashes of the node. Are you monitoring your nodes externally as well to trigger the removal if one of your servers stops responding properly?