0

I want to use python to execute shell commands, how to get the process ID of the process generated by the shell command.

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

The process executed by this shell command does not exit by itself, I need to run 10 min to KILL it, how can I get the id of the process?

W.Darian
  • 65
  • 1
  • 5

1 Answers1

0

You have to store the object Popen returns then use its pid attribute:

p = subprocess.Popen(...)
print(p.pid)
DeepSpace
  • 78,697
  • 11
  • 109
  • 154