Live data from Hacker News

Writing systemd units that stop gracefully before shutdown

psdn.io

21–30 of 57 posts

Re: Writing systemd units that stop gracefully before shutdown

#21
post #8

Earlier quoted context omitted.

I tend to push the metaphor of software being meant to be read and only incidentally to be run by a computer as far as I can. We are telling a story to future us or our successors. Stories have rules and it's jarring when you violate them. 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 nea…

Quoted post unavailable.

If it's not then it's a lot of rambling nonsense instead.

Re: Writing systemd units that stop gracefully before shutdown

#22
post #4

Halt is apparently not the same as poweroff.

Kids of today, etc.

AT power supplies didn't have any mechanism for the system to tell the power supply it wasn't needed any more. So when you shut down the computer, it would wind up at a screen with a message approximating "it is now safe to switch off your computer", at which point the system would halt.

ATX power supplies added the ability for the OS to trigger an actual power off. But that's a different end-state to halting, and if you halt the system then it stays on. You may wonder why anyone would want to halt when power off is an option, and to be honest I'm not entirely sure -- possibly because you have a hardware watchdog which will trigger a reboot of a halted machine but not of a powered off machine?

Re: Writing systemd units that stop gracefully before shutdown

#23
Meta: I can't reach this website!

Chrome is giving me an instant NXDOMAIN error.

Dig shows that

  $ dig psdn.io @1.1.1.1
  ...
  ;; ->>HEADER
so then I prefix "www." like is in the URL...

  $ dig www.psdn.io @1.1.1.1
  ...
  ;; ->>HEADER
Okay, fine:

  $ dig poseidon-www.pages.dev @1.1.1.1
  ...
  ;; ->>HEADER
...wat??

(Where there's no ANSWER section, none was returned, just an AUTHORITY section)

This is reproducible for me with 1.1.1.1, 8.8.8.8 and 9.9.9.9.

Re: Writing systemd units that stop gracefully before shutdown

#24
post #17

A much more challenging task is writing a systemd unit that starts gracefully before shutdown. I wanted to write a unit that could issue an API call to delete the instance rather than doing a normal power off. Putting it at a reasonable place in the sequence took a lot of trial and error! The trick is actually to have the unit be started at some point in normal boot up (e.g. “armed”) and then do the actual task when…

I have the following unit file saved for that purpose: [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.t…

You unit races with the network being brought down, since it isn't listed as a "Before" target, FYI.

Re: Writing systemd units that stop gracefully before shutdown

#25
post #23

Meta: I can't reach this website! Chrome is giving me an instant NXDOMAIN error. Dig shows that $ dig psdn.io @1.1.1.1 ... ;; ->>HEADER so then I prefix "www." like is in the URL... $ dig www.psdn.io @1.1.1.1 ... ;; ->>HEADER Okay, fine: $ dig poseidon-www.pages.dev @1.1.1.1 ... ;; ->>HEADER ...wat?? (Where there's no ANSWER section, none was returned, just an AUTHORITY section) This is reproducible for me with 1.1.1…

Hmm, sorry you're not seeing it. Its just a CNAME to Cloudflare Pages, nothing fancy

  dig www.psdn.io @1.1.1.1
  
  ; > DiG 9.16.33-RH > www.psdn.io @1.1.1.1
  ;; global options: +cmd
  ;; Got answer:
  ;; ->>HEADER

Re: Writing systemd units that stop gracefully before shutdown

#26
post #17

Earlier quoted context omitted.

I have the following unit file saved for that purpose: [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.t…

You unit races with the network being brought down, since it isn't listed as a "Before" target, FYI.

I could be mistaken, but I don't think even that would be sufficient? Before would make your command execute before the network is brought down, but it wouldn't have the latter wait for your command to actually complete.

Re: Writing systemd units that stop gracefully before shutdown

#27
post #8

Earlier quoted context omitted.

I tend to push the metaphor of software being meant to be read and only incidentally to be run by a computer as far as I can. We are telling a story to future us or our successors. Stories have rules and it's jarring when you violate them. 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 nea…

Quoted post unavailable.

Or mushroom output?

Re: Writing systemd units that stop gracefully before shutdown

#28

Earlier quoted context omitted.

You unit races with the network being brought down, since it isn't listed as a "Before" target, FYI.

I could be mistaken, but I don't think even that would be sufficient? Before would make your command execute before the network is brought down, but it wouldn't have the latter wait for your command to actually complete.

The post shows long-running stop scripts / containers and demonstrates them delaying shutdown (not with KillMode none though)

@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...

Re: Writing systemd units that stop gracefully before shutdown

#29
It shouldn't be this hard to stop a service gracefully. This is far, far more complicated than SysV init, where you just need to drop a script into /etc/init.d symlink it from the appropriate rc directories. (For shutdown/reboot, you'd create symlinks in rc5.d and rc6.d named KNNwhatever, where NN is an integer that specifies the order the script will be run in. The "K" stands for "kill".)

Edit: Note that my example runlevels are for Solaris, other UNIX/Linux OSes will vary.

Re: Writing systemd units that stop gracefully before shutdown

#30

A much more challenging task is writing a systemd unit that starts gracefully before shutdown. I wanted to write a unit that could issue an API call to delete the instance rather than doing a normal power off. Putting it at a reasonable place in the sequence took a lot of trial and error! The trick is actually to have the unit be started at some point in normal boot up (e.g. “armed”) and then do the actual task when…

Sadly, I think this would be child's play with SysV init.
Post reply on HN