I have a Python service in systemd. I'd like to have it use the Python syslog module for logging if it's running in systemd vs. otherwise. Is there a reliable way to determine if I'm running in systemd or is there a better way of going about it?
Asked
Active
Viewed 1,603 times
2
jasonwryan
- 73,126
Naftuli Kay
- 39,676
1 Answers
2
systemd will always have a PID of 1, so you can check if the parent PID is 1:
import psutil, os
if psutil.Process(os.getpid()).ppid() == 1:
# We are using systemd
However, it's probally better to offer a command line flag --syslog and pass that with the systemd service, this way the user can select to use syslog even without the systemd service.
user530873
- 257
- 1
- 8
--syslog, then pass that flag with thesystemdservice? – user530873 Apr 05 '16 at 21:31ps -q 1 -o comm=... – jasonwryan Apr 05 '16 at 21:43