Skip to content
systemcoding.dev
Systems Programming

Tell systemd the service is stopping with STOPPING=1

Tell systemd the daemon began shutting down with sd_notify and STOPPING=1, so status shows deactivating while cleanup runs on systemd 253.

1 min read
Summarize in AI

When a daemon takes seconds to drain connections after SIGTERM, systemd cannot tell the difference between "still draining" and "hung" unless you say so. This is an example of the STOPPING=1 notification from sd_notify(3): send it as soon as shutdown starts and systemd knows the shutdown is under way, instead of treating a slow stop as a hang.

stopping.cc
#include <signal.h>
#include <unistd.h>
#include <systemd/sd-daemon.h>

static volatile sig_atomic_t stop;
static void on_term(int sig) { (void)sig; stop = 1; }

int main(void) {
    signal(SIGTERM, on_term);
    sd_notify(0, "READY=1");
    while (!stop)
        pause();
    sd_notify(0, "STOPPING=1\nSTATUS=Draining connections");
    /* ... flush and close ... */
    return 0;
}

The notification does not extend TimeoutStopSec=; if the cleanup can outlast it, send EXTEND_TIMEOUT_USEC= as well. It also only matters for Type=notify units. For a plain Type=simple service the call is harmless but changes nothing. Calling sd_notify from a signal handler is not safe, so the flag is set in the handler and the call is made from the main loop, as above.

Taggedcsystemd
Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.