[systemd-devel] Howto detect a program/service is started by systemd?
Nils Kattenbeck
nilskemail at gmail.com
Mon Jun 30 12:27:06 UTC 2025
Hi,
$INVOCATION_ID is quite a reliable source for this.
I do not what if there are other behavior changes apart from forking you
want to do but it is usually better to do those at a finer resolution than
just a big check for systemd. First and foremost there are also other
service managers which make forking unnecessary, the same hold for running
your program in a container (e.g. Docker) - a cli flag might be a better
option. If you want to use journald, check for it specifically using the
socket or $JOURNAL_STREAM, for socket activation check $LISTEN_FDS, etc.
Cheers, Nils
On Mon, Jun 30, 2025, 13:30 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/9ee38caa/attachment.htm>
More information about the systemd-devel
mailing list