Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从systemctl status的输出中获取单个值_Python_Systemctl - Fatal编程技术网

Python 如何从systemctl status的输出中获取单个值

Python 如何从systemctl status的输出中获取单个值,python,systemctl,Python,Systemctl,如果以上是我的状态输出。我想使用python脚本检索服务名称、正常运行时间和状态。systemctl status有一个允许使用的选项 尝试使用Python可以轻松解析的JSON: [root@localhost etc]# systemctl status blu_av ● blu_av.service - avscan Loaded: loaded (/etc/systemd/system/blu_av.service; disabled; vendor preset: disable

如果以上是我的状态输出。我想使用python脚本检索服务名称、正常运行时间和状态。

systemctl status
有一个允许使用的选项

尝试使用Python可以轻松解析的JSON:

[root@localhost etc]# systemctl status blu_av
● blu_av.service - avscan
   Loaded: loaded (/etc/systemd/system/blu_av.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2018-03-16 16:31:14 IST; 3s ago
 Main PID: 31934 (av)
   CGroup: /system.slice/blu_av.service
           ├─31934 /opt/services/av
           └─31956 /opt/services/av

Mar 16 16:31:14 localhost.localdomain systemd[1]: Started avscan.
Mar 16 16:31:14 localhost.localdomain systemd[1]: Starting avscan...

您可以使用
子流程从systemctl获取输出。请检查\u output()
,然后对其进行解析。

也许您可以尝试解析输出。这是我用过的。请看一看并发表评论

$ sudo systemctl status --output=json-pretty nginx
输出:

import subprocess, re

def read_status(service):
    p =  subprocess.Popen(["systemctl", "status",  service], stdout=subprocess.PIPE)
    (output, err) = p.communicate()
    output = output.decode('utf-8')

    service_regx = r"Loaded:.*\/(.*service);"
    status_regx= r"Active:(.*) since (.*);(.*)"
    service_status = {}
    for line in output.splitlines():
        service_search = re.search(service_regx, line)
        status_search = re.search(status_regx, line)

        if service_search:
            service_status['service'] = service_search.group(1)
            #print("service:", service)

        elif status_search:
            service_status['status'] = status_search.group(1).strip()
            #print("status:", status.strip())
            service_status['since'] = status_search.group(2).strip()
            #print("since:", since.strip())
            service_status['uptime'] = status_search.group(3).strip()
            #print("uptime:", uptime.strip())

    return service_status

def main():
    service = 'mysql'
    reponse = read_status(service)

    for key in reponse:
        print('{}:{}'.format(key, reponse[key]))


if __name__ == '__main__':
    main()

我只是用它来检查我的正则表达式

systemd
服务有一个完整的属性列表。以获取开始时间为例;您可以运行:

service:mysql.service
status:active (running)
since:Fri 2018-03-16 09:17:57 CET
uptime:6h ago
这会产生类似于:

systemctl show your.service --property=ActiveEnterTimestamp
查看所有属性的列表;快跑

ActiveEnterTimestamp=Fri 2019-05-03 09:35:02 CEST

有趣的一步是解析。您将如何执行此操作?@LutzHorn@user9279273只需使用
\n
作为分隔符拆分输出,您将得到一个包含每行输出的数组。从那以后,获得想要的信息应该很容易。那太棒了!
systemctl show your.service