我可以在bash中做到这一点,但我想知道在Python中是否有一种无需使用子进程的方法。
我试过:
if psutil.process(os.getpid()).ppid() == 1
print("systemd")
因为系统没有运气,在其他系统上几乎什么也找不到。
当前shell脚本片段:
if [[ $(/sbin/init --version 2>&1) =~ upstart ]]; then
echo "upstart"
elif [[ $(systemctl) =~ -\.mount ]]; then
echo "systemd"
elif [[ -f /etc/init.d/cron && ! -h /etc/init.d/cron ]]; then
echo "sysv-init"
发布于 2018-07-27 10:34:44
如果要避免运行子流程,可以直接检查进程列表中的指示符:
import psutil
from pathlib import Path
indicators = []
for proc in psutil.process_iter():
name = proc.name()
if name =='systemd':
indicators.append("systemd")
if name =="upstart-udev-bridge":
indicators.append("upstart")
indicators = list(set(indicators))
if len(indicators) == 0:
print("probably sysv-init")
if len(indicators) > 0:
if indicators[0] == 'systemd':
print("probably systemd")
if indicators == 'upstart':
print("probably upstart")
https://stackoverflow.com/questions/-100005839
复制相似问题