[英]Check Linux service status and start it if stopped using a Python script
I have a fixed list of services in a Linux server.我在 Linux 服务器中有一个固定的服务列表。 I want to check the status of these services and start the service if it's stopped.
我想检查这些服务的状态并在服务停止时启动它。 I'm able to get this done using a shell script, but I'm looking for a Python script to do this.
我可以使用 shell 脚本完成此操作,但我正在寻找一个 Python 脚本来执行此操作。
You can use python subprocess.Popen
or os.system
.您可以使用Python
subprocess.Popen
或os.system
。
Example:例子:
subprocess.Popen(["sudo", "service", "redis", "start"])
subprocess.Popen(["sudo", "service", "redis", "status"], stdout=subprocess.PIPE)
You can read the stdout and check if the process is stopped and also see the exist status.您可以读取标准输出并检查进程是否停止并查看存在状态。
Alternatively you can use psutil to monitor processes.或者,您可以使用psutil来监视进程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.