[systemd-devel] Howto detect a program/service is started by systemd?

Mantas Mikulėnas grawity at gmail.com
Mon Jun 30 14:28:00 UTC 2025


No, it really shouldn't detect "started by systemd" at all. These are
several independent things and you shouldn't mix them all together.

Regarding startup, systemd services have several distinct Type= settings
with different expectations and the program has no way to reliably detect
which one is configured. And other init systems exist which also support
handling non-forking services. For example, OpenRC can just as well start
services that don't fork/demonize, and it even supports the systemd-style
"Type=notify" nowadays.

So you shouldn't have a "systemd" mode, but rather have a "--(no-)daemon"
or "--(no-)fork" or "--foreground" option that describes the behavior
expected.

If Type=notify is offered, this can be detected from the specific
$NOTIFY_SOCKET, again separately from which init system is offering it.

On Mon, Jun 30, 2025, 13:26 Stef Bon <stefbon at gmail.com> wrote:

> Hi,
>
> it's important for a program to detect it has been started by systemd.
> For example it does not have to fork since it has already been forked.
>
> I've written a test script, which calls env, and there are a few
> unique environment variables which are an indication it has been
> started by systemd: SYSTEMD_EXEC_PID and INVOCATION_ID.
>
> Is it a good way to test these environment vars (to detect started by
> systemd) or is there a better way?
>
> S. Bon
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/systemd-devel/attachments/20250630/a09acf87/attachment.htm>


More information about the systemd-devel mailing list