我怎样才能让python程序检查linux服务 [英] How can i make the python program to check linux services

查看:13
本文介绍了我怎样才能让python程序检查linux服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作简单的 python 脚本,我可以在 cron 作业上运行它.我只是想看看这些服务当前是在运行还是停止

I want to make simple python script , which i can run on cron job. i just want to see if these services are currently running or stopped

Httpd
mysql

我应该如何用 python 检查它们.

How should i check them with python.

是否需要解析netstat -tlnp

推荐答案

   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程序将检查服务是否正在运行如果服务正在运行,putput 将处于活动状态"如果不是,则为非活动".

This python program will check if the service is running the putput will be "active" if the service is running and "inactive" if not.

希望对你有帮助!!

这篇关于我怎样才能让python程序检查linux服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆