【发布时间】:2011-11-30 14:56:00
【问题描述】:
我正在尝试根据进程 ID 查找进程是否正在运行。根据论坛上的一篇帖子,代码如下。我不能考虑进程名称,因为有多个进程以相同的名称运行。
def findProcess( processId ):
ps= subprocess.Popen("ps -ef | grep "+processId, shell=True, stdout=subprocess.PIPE)
output = ps.stdout.read()
ps.stdout.close()
ps.wait()
return output
def isProcessRunning( processId):
output = findProcess( processId )
if re.search(processId, output) is None:
return true
else:
return False
输出:
1111 72312 72311 0 0:00.00 ttys000 0:00.00 /bin/sh -c ps -ef | grep 71676
1111 72314 72312 0 0:00.00 ttys000 0:00.00 grep 71676
它总是返回 true,因为它可以在输出字符串中找到进程 ID。
有什么建议吗?感谢您的帮助。
【问题讨论】:
-
@Cody:我看的不够远!