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.
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.
#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:
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.