[英]Python script to check server status
I want to check some service status (like MySQL, Apache2) using python. 我想使用python检查某些服务状态(例如MySQL,Apache2)。 I wrote a simple script.
我写了一个简单的脚本。
#!/usr/bin/python
from subprocess import call
command = raw_input('Please enter service name : ')
call(["/etc/init.d/"+command, "status"])
This script will request the user to enter the service and display its results on terminal as follows. 该脚本将要求用户输入服务,并在终端上显示其结果,如下所示。
● apache2.service - LSB: Apache2 web server
Loaded: loaded (/etc/init.d/apache2)
Drop-In: /lib/systemd/system/apache2.service.d
└─apache2-systemd.conf
Active: active (running) since සි 2016-06-17 09:16:10 IST; 5h 43min ago
Docs: man:systemd-sysv-generator(8)
Process: 2313 ExecReload=/etc/init.d/apache2 reload (code=exited, status=0/SUCCESS)
Process: 1560 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/apache2.service
├─1941 /usr/sbin/apache2 -k start
├─2332 /usr/sbin/apache2 -k start
├─2333 /usr/sbin/apache2 -k start
├─2334 /usr/sbin/apache2 -k start
├─2335 /usr/sbin/apache2 -k start
└─2336 /usr/sbin/apache2 -k start
I just want to take this line for each service Active: active (running)
and check if that is running and if not I want to ask do you want to start it. 我只想对每项服务
Active: active (running)
此行,并检查该服务是否正在运行,如果不是,我想问一下您是否要启动它。
Can some one help me to do this? 有人可以帮我做到这一点吗? Thanks in advance
提前致谢
I think what you want is to capture the output. 我认为您想要的是捕获输出。 Something along the lines of:
类似于以下内容:
status = subprocess.check_output("YOUR COMMAND", shell=True)
if ("Active: active (running)" in status):
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.