我如何使python程序检查linux服务

我如何使python程序检查linux服务

本文介绍了我如何使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

推荐答案

运行ps shell参数:

You could use the commands module to run the ps shell argument:

import commands
output = commands.getoutput('ps -A')
if 'Httpd' in output:
    print("Httpd is up an running!")


$ b b

我刚刚读了命令模块,虽然工作正常,它似乎已被弃用自Python 2.6,甚至在Python 3.x中删除。因此,如果你使用Python 3或者想要将来移植到Python 3,你可以使用subprocess模块​​:

[edit] I just read up on the commands module, and although working fine, it seems to be deprecated since Python 2.6 and even removed in Python 3.x. So if you are on Python 3 or want to be ready for future porting to Python 3, you can use the subprocess module:

import subprocess
output = subprocess.check_output(['ps', '-A'])
if 'Httpd' in output:
    print("Httpd is up an running!")

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

07-24 01:50