[英]What is the best way to see if a python process is already running?
I'm trying to write a script that monitors my python program to see if it is already running. 我正在尝试编写一个脚本来监视我的python程序,以查看它是否已在运行。
I started by trying to assign this script to an object: 我首先尝试将此脚本分配给一个对象:
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"
I then tried to monitor the object for more than one instance. 然后,我尝试监视该对象的多个实例。
There are Linux commands that return a list of processes: 有一些Linux命令可返回进程列表:
if 'your_process' in commands.getoutput('ps -A'):
print 'Your process in running.'
Subprocess's Popen is very useful for this. 子进程的Popen为此非常有用。 https://docs.python.org/3.4/library/subprocess.html 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.")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.