我正在尝试编写一个监视我的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"
然后我尝试监视多个实例的对象。
答案 0 :(得分:3)
您可能需要查看psutils来处理流程。
import psutil
processWatch = [p.cmdline() for p in psutil.process_iter()].count(['python', 'quickConversion.py'])
if processWatch > 0:
`while 1: `
`"rest of loop"`
答案 1 :(得分:2)
有一些Linux命令可以返回进程列表:
if 'your_process' in commands.getoutput('ps -A'):
print 'Your process in running.'
答案 2 :(得分:-1)
Subprocess的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.")