1

I'm starting a program from python script using subprocess.Popen:

cmd = "exec sudo timeout 120 /opt/something/my_program"
self.process = subprocess.Popen(cmd, shell=True)

As a result I get three processes, eg.:

24144 pts/0    S+     0:00 sudo timeout 120 /opt/something/my_program
24146 pts/0    S      0:00 timeout 120 /opt/something/my_program
24147 pts/0    Sl     0:00 /opt/something/my_program

When I try to send SIGTERM to parent process (in this case 24144) from command line everything works fine and processes are killed almost immediately every time. This works fine from command line:

sudo kill -15 <pid>

But when I'm trying to use that command in python script it works only sometimes. Commands I already tried:

cmd = "sudo kill -15 " + pid
os.system(cmd)
subprocess.call(cmd, shell=True)
subprocess.Popen(cmd, shell=True)

So my question is: why is it happening? What is the difference between command line and python in this case? And how can I fix this?

pykaczka
  • 29
  • 5
  • try using an array for the `cmd = ["sudo", "kill", "-15", pid]` then `subprocess.Popen(cmd, shell=True)` – DMin Jul 20 '21 at 12:37

0 Answers0