Here’s the unit I ended up with: https://github.com/CGamesPlay/infra/blob/master/private-serv...
Here’s the unit I ended up with: https://github.com/CGamesPlay/infra/blob/master/private-serv...
There's an idea in software that's a bit like the corollary of Chekhov's gun. Chekhov's gun is about not presaging jarring story elements that will never come to pass. But it's nearly as jarring to leave important story arcs as complete surprises until the end. Producing the gun moments before the curtain goes down would be quite a WTF. We didn't know that was a possibility. That's a niche that some people occupy, but it is a niche.
Introducing things early fights with Locality of Reference, but when we're talking about things of deep, dramatic importance (like an attempted murder, or a reaper process) it's important to introduce that "character" early in the story so that people know that it exists. Failing to do so is a form of deus ex machina and we only appreciate that in very small doses.
So framed that way, I don't see a problem with having to start a killswitch while you're spinning everything up. It's there, people can see it, and know to ask questions about it.
[Unit]
# https://stackoverflow.com/questions/36729207/trigger-event-on-aws-ec2-instance-stop-terminate
Description=unlink agent from remote server
Before=shutdown.target
[Service]
Type=oneshot
EnvironmentFile=-/etc/environment
KillMode=none
ExecStart=/bin/true
ExecStop=/opt/service-name/shutdown-unlink
RemainAfterExit=yes
User=root
[Install]
WantedBy=multi-user.target
If I recall correctly, the KillMode=none is important as it causes the shutdown-unlink binary to escape systemd process supervision. Without it, you may deal with systemd immediately halting your shutdown unit (and killing the process) when it hits the shutdown target.@CGamesPlay network.target's "primary purpose is for ordering things properly at shutdown: since the shutdown ordering of units in systemd is the reverse of the startup ordering, any unit that is order After=network.target can be sure that it is stopped before the network is shut down if the system is powered off."
https://www.freedesktop.org/wiki/Software/systemd/NetworkTar...
The shutdown order is the reverse of startup order, and they execute the payload on shutdown (ExecStop).
"KillMode=none" shouldn't be necessary, unless your command leaves processes running that need to stay running (and in that case you have other problems, since those processes may not actually finish before the system is shut down). Systemd waits for your service to stop before reaching shutdown.target (per the default Before= and Conflicts= dependencies).
Since your command probably needs networking, you also want "After=network.target" to ensure your command runs before network is torn down, as covered in sibling comments.