News for hackers. Stuff that matters.
Re:Show HN: Interactive systemd (a better way to work with s... by yread on Saturday January 18, 2025 @12:27PM (#42749839)
ExecStop works the way you want for type=forking
Re:Show HN: Interactive systemd (a better way to work with s... by ww520 on Saturday January 18, 2025 @12:40PM (#42749931)
Still has the same problem even with type=forking. Only way to get it working was RemainAfterExit=yes
Re:Show HN: Interactive systemd (a better way to work with s... by shawnz on Saturday January 18, 2025 @12:42PM (#42749941)
I think you actually probably want type=oneshot (and also RemainAfterExit=yes) for the kind of service you're describing
Re:Show HN: Interactive systemd (a better way to work with s... by brirec on Saturday January 18, 2025 @12:52PM (#42750005)
This was ultimately what I needed to do when I wrote a systemd service that managed some firewall rules. It really was a footgun though, what with having essentially different meanings/purposes for ExecStop whether you’re doing a Type=forking, a Type=oneshot, or a Type=oneshot with RemainAfterExit=yes.

And relatedly, I honestly have no idea when I’d want to use ExecStartPre, or multiple ExecStarts, or ExecStartPost, and so on.

Re:Show HN: Interactive systemd (a better way to work with s... by ww520 on Saturday January 18, 2025 @01:34PM (#42750301)
Having different semantics with different proprieties on the same command is really confusing.
Re:Show HN: Interactive systemd (a better way to work with s... by yencabulator on Saturday January 18, 2025 @03:56PM (#42751359)
Think of it as an enum, Type branches the logic.

    enum Service {
        Exec { ExecStart: String, ... }
        Forking { ExecStart: String, ... }
        OneShot { ExecStart: String, ... }
    }
You can argue that sometimes that ExecStart could be a different term, but it'd still end up being the same across multiple enum variants.
Re:Show HN: Interactive systemd (a better way to work with s... by ww520 on Saturday January 18, 2025 @07:06PM (#42752471)
Yes. ExecStart works the same for all the cases. ExecStop works differently though. While ExecStart is the event to kick off the command for the service, ExecStop is not. The asymmetric semantics are where the confusion comes from.
Re:Show HN: Interactive systemd (a better way to work with s... by shawnz on Saturday January 18, 2025 @01:44PM (#42750379)
I would argue the semantics of ExecStop are always the same. It's the command that's executed to stop the service. On the other hand, what it means for a service to be "running" or "stopping" naturally depends on what type of service it is (i.e., is it a daemon or not?)
Re:Show HN: Interactive systemd (a better way to work with s... by ww520 on Saturday January 18, 2025 @02:13PM (#42750617)
> the command that's executed to stop the service

That’s what is assumed. But in reality it runs after the started process stops.

Re:Show HN: Interactive systemd (a better way to work with s... by shawnz on Saturday January 18, 2025 @02:37PM (#42750784)
Yes, so whether the service is stopping as a result of the process exiting, or whether you requested the service to stop manually, it will run the ExecStop in either case.

That makes sense to me personally. What would be the more intuitive design in your mind?

Re:Show HN: Interactive systemd (a better way to work with s... by ww520 on Saturday January 18, 2025 @07:08PM (#42752491)
Stopping as a result of the process exiting or requested the service to stop are two very different things. Systemd overloads the term ExecStop for different semantics, relying on different property settings. That's where the confusion comes from.
Re:Show HN: Interactive systemd (a better way to work with s... by drougge on Saturday January 18, 2025 @10:10PM (#42753250)
The name sounds like it means "this is how I want you to cause the service to stop" to me (and clearly to others as well). That would be symmetrical with ExecStart meaning "this is how I want you to cause the service to start". If it runs after the service stopped it should be called "ExecAfterStop" or something like that.
Re:Show HN: Interactive systemd (a better way to work with s... by shawnz on Saturday January 18, 2025 @11:43PM (#42753709)
That is what ExecStop means. It specifies how you want to cause the service to stop. But the lifetime of the service isn't exactly the same thing as the lifetime of the process that got started in ExecStart.

Maybe think about it this way: ExecStart is what the system will run to transition the service from the "starting" state to the "started" state. ExecStop is what the system will run to transition the service from the "stopping" state to the "stopped" state.

For a service with RemainAfterExit=no (the default), you enter the stopping state right away once the processes that got started in ExecStart exit. That's useful when you are starting some long lived process as a service, and in that case there is usually no need for an ExecStop. But semantically, ExecStop has the same meaning either way -- it's what needs to be run, if anything, to transition the service from the stopping state to the stopped state.

Re:Show HN: Interactive systemd (a better way to work with s... by drougge on Sunday January 19, 2025 @09:22AM (#42757219)
I have now found the documentation for ExecStop (in systemd.service(5)), which hopefully improves my understanding.

It definitely seems to be both "cause to stop" and "after (unexpected) stop" in one. You can look at $MAINPID to see which case you have. This design apparently makes sense to you, but to me and several others in this thread a service that has already stopped isn't in need of being stopped and shouldn't execute commands intended for that. (There is a separate ExecStopPost for "after stopping, for any reason".)

Re:Show HN: Interactive systemd (a better way to work with s... by BenjiWiebe on Sunday January 19, 2025 @09:11AM (#42757123)
It's been enlightening to me to read through some of the distro-provided .service files to see what can be done, with services I'm more of less familiar with.