文章内容

2022/1/8 22:53:55,作 者: 黄兵

Linux 如何监控服务


下面的代码展示了如何确定 Linux 服务器上某个服务是否在运行:

import subprocess


service = "apache2"

p =  subprocess.Popen(["systemctl", "is-active",  service], stdout=subprocess.PIPE)
(output, err) = p.communicate()
output = output.decode('utf-8')

print(output)

这个 python 程序将检查服务是否正在运行,如果服务正在运行,output 将是“activate”,如果不是,则将是“inactive”。


参考资料:

1、How can i make the python program to check linux services

分享到:

发表评论

评论列表