擅长:python、mysql、java
<p><a href="https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess">Here is a good explaination of shell=True</a></p>
<p>将shell参数设置为true值会导致子进程生成一个中间shell进程,并告诉它运行命令</p>
<p>总是使用=shell=False,但是您的代码在我的linux环境中运行如下,为了简单起见,我使用shell文件作为命令来运行]:</p>
<pre><code>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'
</code></pre>