我正在尝试编写一个脚本来监视我的python程序,看看它是否已经在运行。
我首先尝试将此脚本分配给一个对象:
processWatch = os.system("sudo ps afxj | grep quickConversion.py")
if processWatch > 2: #if more than one is running it won't run.
while 1:
"rest of loop"然后,我尝试监视该对象的多个实例。
发布于 2015-03-27 22:35:02
您可能希望查看一下psutils来处理进程。
import psutil
processWatch = [p.cmdline() for p in psutil.process_iter()].count(['python', 'quickConversion.py'])
if processWatch > 0:
`while 1: `
`"rest of loop"`发布于 2015-03-27 22:22:16
有一些Linux命令可以返回进程列表:
if 'your_process' in commands.getoutput('ps -A'):
print 'Your process in running.'发布于 2015-03-27 22:26:03
子进程的Popen对此非常有用。https://docs.python.org/3.4/library/subprocess.html
import subprocess
process = subprocess.Popen(cmd)
if process.poll() is None:
print("The process hasn't terminated yet.")https://stackoverflow.com/questions/29302839
复制相似问题