from Hacker News

Writing systemd units that stop gracefully before shutdown

by dghubble on 10/26/22, 4:14 PM with 53 comments

  • by CGamesPlay on 10/26/22, 6:32 PM

    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 the unit is stopped.

    Here’s the unit I ended up with: https://github.com/CGamesPlay/infra/blob/master/private-serv...

  • by SoftTalker on 10/26/22, 8:12 PM

    I fundamentally disagree with the idea that software should require or even expect a graceful shutdown. You can never stop the user from yanking the power cord out of the socket, which is what they will do if you force a bunch of housekeeping to happen before shutdown.

    You have to deal with crash/power failure recovery anyway. So do your housekeeping on startup. Shutdown should be a quick and simple termination.

  • by akeck on 10/26/22, 5:20 PM

    I love this. Lots of details I didn't know.
  • by MarkusWandel on 10/27/22, 12:37 PM

    Quoting from the article:

       TimeoutStopSec=0
    
    That's cost me more than one hard power button powerdon (on a desktop machine with the system partition on SSD - unnerving).

    One of the innumerable things that systemd stops on shutdown gets stuck - permanently - and the machine goes into a state out of which, to my knowledge, is only a powerdown or reset.

    I ended up searching for the above and replacing them with a reasonable timeout (several minutes).

  • by rfmoz on 10/27/22, 7:09 AM

    The macOS init manager, LaunchD, doesn't offer an easy way to execute an script at shutdown.

    By definition, it sends a SIGTERM signal to all of the daemons that it started. But as the script isn't started before, and doesn't keep a running PID, you don't have a clean way to do it.

    I don't understand why they only implemented the SIGTERM call without any alternative.

  • by SrslyJosh on 10/26/22, 10:01 PM

    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.

  • by kzrdude on 10/26/22, 6:03 PM

    Halt is apparently not the same as poweroff.
  • by exikyut on 10/26/22, 9:18 PM

    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<<- opcode: QUERY, status: NOERROR, id: 19283
      ...
      ;; QUESTION SECTION:
      ;psdn.io.                       IN      A
    
    so then I prefix "www." like is in the URL...

      $ dig www.psdn.io @1.1.1.1
      ...
      ;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 64024
      ...
      ;; QUESTION SECTION:
      ;www.psdn.io.                   IN      A
      
      ;; ANSWER SECTION:
      www.psdn.io.            300     IN      CNAME   poseidon-www.pages.dev.
    
    Okay, fine:

      $ dig poseidon-www.pages.dev @1.1.1.1
      ...
      ;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 55471
      ...
      ;; QUESTION SECTION:
      ;poseidon-www.pages.dev.                IN      A
    
    ...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.