Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的进程执行检查和获取PID_Python_If Statement_Subprocess_Popen_Pid - Fatal编程技术网

Python中的进程执行检查和获取PID

Python中的进程执行检查和获取PID,python,if-statement,subprocess,popen,pid,Python,If Statement,Subprocess,Popen,Pid,我需要在后台运行bash命令,但稍后需要将其杀死(os.kill())。我还想确保命令运行我有这个来确保命令运行 if subprocess.Popen("tcpdump -i eth0 -XX -w /tmp/tmp.cap &", shell=True).wait() == 0: 我不确定如何更改它,以便在仍然能够检查执行是否成功的情况下使用Popen.pid获取pid。 任何帮助都将不胜感激。 谢谢。使用该方法。您还可以获取Popen.returncode,以确定流程是否成功完成

我需要在后台运行bash命令,但稍后需要将其杀死(os.kill())。我还想确保命令运行我有这个来确保命令运行

if subprocess.Popen("tcpdump -i eth0 -XX -w /tmp/tmp.cap &", shell=True).wait() == 0:
我不确定如何更改它,以便在仍然能够检查执行是否成功的情况下使用Popen.pid获取pid。 任何帮助都将不胜感激。 谢谢。

使用该方法。您还可以获取
Popen.returncode
,以确定流程是否成功完成

import subprocess

tasks = [subprocess.Popen('ping www.stackoverflow.com -n 5 && exit 0', shell=True),
         subprocess.Popen('ping www.stackoverflow.com -n 5 && exit 1', shell=True)]

for task in tasks:
    while task.poll() is None:
        # the task has not finished
        pass

    print task
    print task.pid
    print task.returncode

要启动子流程,请等待一段时间并终止它,并检查其退出状态是否为零:

import shlex
from subprocess import Popen
from threading import Timer

def kill(process):
    try:
        process.kill()
    except OSError: 
        pass # ignore

p = Popen(shlex.split("tcpdump -i eth0 -XX -w /tmp/tmp.cat"))
t = Timer(10, kill, [p]) # run kill in 10 seconds
t.start()
returncode = p.wait()
t.cancel()
if returncode != 0:
   # ...
或者您也可以自己实现超时:

import shlex
from subprocess import Popen
from time import sleep, time as timer # use time.monotonic instead

p = Popen(shlex.split("tcpdump -i eth0 -XX -w /tmp/tmp.cat"))

deadline = timer() + 10 # kill in 10 seconds if not complete
while timer() < deadline:
    if p.poll() is not None: # process has finished
        break 
    sleep(1) # sleep a second
else: # timeout happened
    try:
        p.kill()
    except OSError:
        pass

if p.wait() != 0:
   # ...
导入shlex
从子流程导入Popen
从时间导入睡眠,时间作为计时器#用时间代替单调
p=Popen(shlex.split(“tcpdump-i eth0-XX-w/tmp/tmp.cat”))
截止日期=计时器()+10#如果未完成,请在10秒内终止
当计时器()小于截止日期时:
如果p.poll()不是None:#进程已完成
打破
睡觉(1)#睡一会儿
否则:#发生超时
尝试:
p、 杀死
除操作错误外:
通过
如果p.wait()!=0:
# ...
它假定
睡眠
使用与
定时器
类似的时钟


threading.Timer
variant允许您的代码在子进程退出后立即继续运行。

您可以使用变量
p=Popen(…)
p.kill()
--终止,
returncode=p.wait()
--等待完成。顺便说一句,你不需要在这里使用
shell=True
:把
shell=True
&
放在末尾,然后使用
p=Popen(shlex.split(cmd))
是的,我不确定我是否需要shell=True,或者&但是它对他们有效,我会尝试你的建议,谢谢你的时间。OP启动单个进程<代码>任务。返回代码检查是多余的<代码>非任务。poll()错误:它应该是
任务。poll()为None
——进程仍在运行。一般来说,应该避免繁忙循环(您可以在检查之间暂停)。@J.F.Sebastian,我理解OP启动单个进程,而不是
ping
。。。OP想知道进程是否成功终止,为了演示该功能,两个示例进程以不同的退出代码终止。感谢您的回复,如果我的问题误导了您,我很抱歉,我想检查进程是否成功执行,但进程应该在后台运行,直到终止。如果没有安装TCPDump,它将无法运行,我想知道这一点。感谢您的回复。
Popen.wait()
不允许进程按照OP的请求“在后台运行”。@Graham:
。只有在进程已经完成(在第二个示例中)或被终止后才会调用wait()
。在这两种情况下,进程在10秒后都无法生存。“背景”是来自shell的术语。这里没有贝壳
Popen
在“后台”运行所有进程,除非您的意思是要创建unix守护进程(OP不太可能需要它)