Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a program detect if it is running as a systemd daemon?

Tags:

linux

systemd

Is there any way to detect in a program that it is run by systemd as a daemon?

systemd API

sd_booted()

is used to detected if the whole system is booted by systemd, but says nothing about the program itself.

Thanks

like image 866
Ralph Zhang Avatar asked Sep 07 '16 11:09

Ralph Zhang


2 Answers

Get the parent process id and see whether that process is systemd.

like image 148
Maxim Egorushkin Avatar answered Nov 16 '22 01:11

Maxim Egorushkin


You could set a magic environment variable in the daemon's service file and look for this variable.

like image 41
Umut Avatar answered Nov 16 '22 01:11

Umut