首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何判断系统在python中使用的是systemd、sysv-init还是upstart?

如何判断系统在python中使用的是systemd、sysv-init还是upstart?
EN

Stack Overflow用户
提问于 2018-07-27 00:48:40
回答 1查看 0关注 0票数 0

我可以在bash中做到这一点,但我想知道在Python中是否有一种无需使用子进程的方法。

我试过:

代码语言:txt
复制
 if psutil.process(os.getpid()).ppid() == 1 
        print("systemd")

因为系统没有运气,在其他系统上几乎什么也找不到。

当前shell脚本片段:

代码语言:txt
复制
    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"
EN

回答 1

Stack Overflow用户

发布于 2018-07-27 10:34:44

如果要避免运行子流程,可以直接检查进程列表中的指示符:

代码语言:txt
复制
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")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档