简体   繁体   English

有没有办法编写一个脚本,每隔几分钟检查另一个 python 进程是否正在运行并在必要时重新运行它?

[英]Is there a way to write a script that checks every few minutes if another python process is running and reruns it if necessary?

I am aware how to check if some python process is running.我知道如何检查某些 python 进程是否正在运行。 I am trying to write a script, which checks whether a python script is running, if it is not it should rerun it.我正在尝试编写一个脚本,它检查 python 脚本是否正在运行,如果不是,它应该重新运行它。

What I have right now is:我现在拥有的是:

import os


stream = os.popen("ps aux | grep combined.py")
output = stream.read()
print(output[0])

The problem is I can't get the specific process ID this way, because output is a list of characters not a dict, where I could get PID by output["PID"], to check whether there is an PID in the list.问题是我无法通过这种方式获取特定的进程 ID,因为output是字符列表而不是字典,我可以通过输出 [“PID”] 获取 PID,以检查列表中是否存在 PID。

How would I implement such script?我将如何实现这样的脚本?

In bash script:在 bash 脚本中:

#!/bin/bash
pid=`ps -ef |grep combined.py |grep -v grep |awk '{print $2}'`
echo $pid

You can use crontab to run the bash script and checks every few minutes if the python process is running您可以使用 crontab 运行 bash 脚本并每隔几分钟检查一次 python 进程是否正在运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM