I need to run a script before a set of other services on my machine stop when shutting the machine down. (In my case, terminating an EC2 instance.)
I have tried variations on the answer in this question but nothing works: How do I run a script before everything else on shutdown with systemd? (I also looked at unix.se, but it's also no good: https://unix.stackexchange.com/questions/39226/how-to-run-a-script-with-systemd-right-before-shutdown… and I looked in mailing lists too.)
My biggest problem is that the OP of that question wanted to run the script before some mounts were unmounted but I don't have any requirements on mounts, but rather on services.
My service is something like this:
[Unit]
Description=Run script before other things stop on shutdown
Wants=multi-user.target network.target foo.service bar.service
After=multi-user.target network.target foo.service bar.service
[Service]
ExecStart=/bin/true
ExecStop=-/my/script
ExecStopPost=/bin/sleep 5s
RemainAfterExit=yes
Type=oneshot
[Install]
WantedBy=multi-user.target
I enable and start this unit after boot, but it doesn't seem to run the script when I shut the instance down. I'm at a loss, how can I do this?
I am using systemd 232 on Debian Stretch.
Best Answer
Perhaps the ExecStop isn't running because the ExecStart process has already terminated?
If that's the case, set
ExecStart=/bin/sleep infinity
.