请问一下,在 python中使用Popen模块的问题。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from subprocess import Popen,PIPE
import os,signal

def getPid():
    p = Popen("netstat -lntup|grep prometheus|awk -F '[ |/]+' '{print $7}'", shell=True,stdout=PIPE, stderr=PIPE)
    pid=str(p.communicate()[0],encoding='utf-8').replace('\n','')
    return pid

def killProcess(strpid):
    if strpid.isdigit():
        pid=int(strpid)
        os.system("kill -9 "+strpid)
    else:
        print("The pid of prometheus is not exist")

def launchProcess():
    pid=getPid()
    if pid.isdigit():
        killProcess(pid)
    p2=Popen("daemonize -c /opt/modules/prometheus /opt/modules/prometheus/prometheus",shell=True,stdout=PIPE, stderr=PIPE)
    p1=Popen("netstat -lntup|grep prometheus", shell=True,stdout=PIPE, stderr=PIPE)
    print(p1.communicate(),p2.communicate())

if __name__=='__main__':
    launchProcess()

 

 

这里launchProcess方法里面虽然前面kill了一下,但后面又开了两个子进程,p2和p1为啥获取不到子进程的值了。感谢各位大神。

分析思路: 版本差异