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

Simon McVittie smcv at collabora.com
Mon Aug 4 12:11:09 UTC 2025


On Mon, 30 Jun 2025 at 14:27:06 +0200, Nils Kattenbeck wrote:
> $INVOCATION_ID is quite a reliable source for [whether a
> program/service was started by systemd].

This is not reliable, because of how environment variables inherit from parent
to child. If the INVOCATION_ID is set, all this tells you is that either your
program or some other program "above" you was started by systemd: it does not
tell you that it was, specifically, your program that was started like this. If
you have a chain of subprocesses where a process was started by systemd, and it
started your program further down the process tree, like traditional D-Bus
activation:

systemd
   \- dbus-daemon [*]
       \- maybe dbus-daemon-launch-helper
            \- your-program

or a session manager:

systemd --user
   \- gnome-session [*]
        \- your-program

or a GUI app started as a child of a GUI environment:

systemd --user
   \- gnome-shell [*]
        \- firefox
             \- your-program

the the INVOCATION_ID that was set for the service marked [*] will be
inherited by your program, unless the service marked [*] has intentionally
taken steps to exclude it from the environment.

dbus-daemon *does* remove INVOCATION_ID from the activation environment when
using traditional activation, but this is unusual, and only happens because
code was explicitly written to do that. Most programs that might start some
other program as a subprocess, like for example Firefox opening a downloaded
file with the default MIME handler, will not do that.

    smcv


More information about the systemd-devel mailing list