擅长:python、mysql、java
<p><code>Popen</code>返回具有<code>pid</code>的对象。根据<a href="https://docs.python.org/3/library/subprocess.html#subprocess.Popen" rel="nofollow noreferrer">doc</a></p>
<blockquote>
<p>Popen.pid
The process ID of the child process.</p>
<p>Note that if you set the shell argument to True, this is the process ID of the spawned shell.</p>
</blockquote>
<p>您需要关闭<code>shell=True</code>来获取进程的pid,否则它会给出shell的pid。你知道吗</p>
<pre><code>args = 'ping -c 1 1.2.3.4'
while True:
time.sleep(60)
with subprocess.Popen(args, shell=False, stdout=subprocess.PIPE) as proc:
print('PID: {}'.format(proc.pid))
...
</code></pre>