문제

I need to run a bash command in the background but then need to kill(os.kill()) it later. I also want to make Sure the command runs I have this to make sure the command runs.

if subprocess.Popen("tcpdump -i eth0 -XX -w /tmp/tmp.cap &", shell=True).wait() == 0:

I'm not sure on how to change this so i can use Popen.pid to get the pid while still being able to check if execution was successful. Any help would be appreciated. Thanks.

도움이 되었습니까?

해결책

To start a subprocess, wait some time and kill it, and check that its exit status is zero:

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:
   # ...

Or you could implement the timeout yourself:

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:
   # ...

It assumes sleep uses similar clock as timer.

threading.Timer variant allows your code to continue as soon as the subprocess exits.

다른 팁

Use the Popen.poll() method. You can get the Popen.returncode as well to determine if the processes completed successfully.

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top