Skip to content
systemcoding.dev
Systems Programming

Report progress text to systemd with sd_notify STATUS=

Send free-form progress text to systemctl status with sd_notify and STATUS=, available in libsystemd on systemd 253 and later.

1 min read
Summarize in AI

A long start-up is easier to debug when systemctl status says what the daemon is doing right now. This is an example of the STATUS= field of the notify protocol, which sd_notify(3) describes as free-form text shown in the status output. The unit must use Type=notify, otherwise systemd has no notification socket for the service and the call silently does nothing.

status.cc
#include <systemd/sd-daemon.h>

int main(void) {
    sd_notify(0, "STATUS=Loading 1200 records");
    /* ... load the records ... */
    sd_notify(0, "READY=1\nSTATUS=Serving on port 8080");
    return 0;
}

After the second call the unit status contains this line:

Text
     Status: "Serving on port 8080"

You can combine READY=1 and STATUS= in one string, separated by a newline, so both reach systemd in a single datagram. The text replaces the previous status, it is not appended. Link with -lsystemd. When the process runs outside systemd, NOTIFY_SOCKET is unset and sd_notify returns 0, so the same binary works from a shell, though a status string is lost there. Avoid putting secrets in it, because any user can read the status of a unit.

Taggedcsystemd
Share

More snippets

Systems Programming

Get the page size with sysconf(_SC_PAGESIZE)

Read the memory page size at run time with sysconf(_SC_PAGESIZE) instead of hard-coding 4096, which is wrong on arm64 kernels built with 16K or 64K pages.

1 min read

Get new posts by email

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

Unsubscribe in one click. No spam, ever.