我正在尝试根据进程ID来查找进程是否正在运行。代码如下,基于论坛上的帖子之一。我不能考虑进程名称,因为有多个进程使用相同的名称运行。检查一个进程是否正在Python中运行(在Linux/Unix中)
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:我没有看远远不够去看看! –