在后台启动另一个进程并在Python中捕获输出

时间:2018-03-26 13:23:11

标签: python python-2.7 stdout spawn

在python中我想启动另一个python脚本作为后台进程,几秒后我需要杀死那个生成的进程并将stdout变为变量。

我尝试使用subprocess.Popen,我可以作为后台进程生成并在几秒后终止。但是,在将stdout重定向到变量时,它会被阻止。

有人建议我修理它吗?或者除subprocess.Popen以外还有其他模块可以执行此操作吗?

g_flag = 0
class StartChecking(Thread):
    def __init__(self):
        Thread.__init__(self)
    def run(self):
        global g_flag
        print 'Starting thread'
        proc = subprocess.Popen(["./cmd.py"], stdout=subprocess.PIPE, shell=True)
        pid = proc.pid
        print 'Sniff started ' + str(pid)
        if (pid != 0) and (pid != 1):
            while g_flag == 0:
                sleep(0.1)
            os.kill(pid, signal.SIGTERM)
            print 'Killed ' + str(pid)
        (out, err) = proc.communicate() # Currently its blocking here
        print out


th = StartChecking()
th.start()
#Do something else
sleep(5)
g_flag = 1
th.join()
print 'thread joined'

输出

Starting thread
Sniff started 24294
Killed 24294

注意:在Python 2.7.12中使用ubuntu 16.04

2 个答案:

答案 0 :(得分:0)

Here is a good explaination of shell=True

将shell参数设置为true值会导致子进程生成中间shell进程,并告诉它运行命令

总是使用= shell = False,你的代码可以在我的linux环境下工作,如下所示,为简单起见,我使用shell文件作为运行命令]:

import os,signal
import time
import subprocess
from threading import Thread

g_flag = 0
class StartChecking(Thread):
    def __init__(self):
        Thread.__init__(self)
    def run(self):
        global g_flag
        print 'Starting thread'
        proc = subprocess.Popen(["./a.sh"], stdout=subprocess.PIPE, shell=True)
        pid = proc.pid
        print 'Sniff started ' + str(pid)
        if (pid != 0) and (pid != 1):
            while g_flag == 0:
                time.sleep(0.1)
            os.kill(pid, signal.SIGTERM)
            print 'Killed ' + str(pid)
        (out, err) = proc.communicate() # Currently its blocking here
        print out


th = StartChecking()
th.start()
#Do something else
time.sleep(5)
g_flag = 1
th.join()
print 'thread joined'

答案 1 :(得分:0)

删除shell=True后,它开始工作了。并且在没有线程的情况下重构了代码。如果我添加shell=True,则会在communicate来电中再次开始阻止。

proc = subprocess.Popen(["./cmd.py"], stdout=subprocess.PIPE)
#Do something else
sleep(2)
os.system('kill -15 ' + str(proc.pid))
print 'Killed ' + str(proc.pid)
print 'cmd out: ' + proc.communicate()[0]
print 'finished'