【问题标题】:Python executes the shell command, how to get the process ID of the process generated by the shell command [duplicate]Python执行shell命令,如何获取shell命令生成的进程的进程ID [重复]
【发布时间】:2020-01-20 04:33:29
【问题描述】:

我想用python执行shell命令,如何获取shell命令生成的进程的进程ID。

import subprocess
subprocess.Popen(['{}'.format(self.executor), '-c', {}'.format(config), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

这个shell命令执行的进程不会自行退出,需要运行10分钟才能KILL它,如何获取进程的id?

【问题讨论】:

    标签: python shell


    【解决方案1】:

    你必须存储对象Popen返回然后使用它的pid属性:

    p = subprocess.Popen(...)
    print(p.pid)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-13
      • 2016-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-24
      相关资源
      最近更新 更多