【问题标题】:how to get individual values from the output of systemctl status如何从 systemctl status 的输出中获取单个值
【发布时间】:2018-08-25 13:38:28
【问题描述】:
[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...

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

【问题讨论】:

    标签: python systemctl


    【解决方案1】:

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

    systemctl show your.service --property=ActiveEnterTimestamp
    

    这会给出类似的东西:

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

    查看所有属性的列表;跑吧

    systemctl show your.service
    

    【讨论】:

      【解决方案2】:

      systemctl status 有一个选项--output 允许使用journalctl 的选项。

      试试 Python 可以轻松解析的 JSON:

      $ sudo systemctl status --output=json-pretty nginx
      

      【讨论】:

        【解决方案3】:

        也许你可以尝试regular expression 来解析输出。这是我用的。请查看并发表评论。

        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()
        

        输出:

        service:mysql.service
        status:active (running)
        since:Fri 2018-03-16 09:17:57 CET
        uptime:6h ago
        

        我只是使用this 来检查我的正则表达式。

        【讨论】:

        • 太棒了!
        【解决方案4】:

        您可以使用subprocess.check_output() 从 systemctl 获取输出,然后对其进行解析。

        【讨论】:

        • 有趣的步骤是解析。你会怎么做?
        • @LutzHorn @user9279273 只需将输出以\n 作为分隔符,您将得到一个包含每一行输出的数组。从那里开始应该很容易获得所需的信息。
        猜你喜欢
        • 1970-01-01
        • 2020-03-27
        • 2015-12-04
        • 2019-11-05
        • 2010-11-05
        • 2022-08-24
        • 1970-01-01
        • 1970-01-01
        • 2020-04-12
        相关资源
        最近更新 更多