1

I am trying to kill a subprocess via its pid by using subprocess.call() to do it. I obtain the pid by assigning return to a value like this:

return = subprocess.Popen(["sudo", "scrolling-text-example", "-y7"]) 
x= return.pid

When when I am ready to end this subprocess I am using this code:

subprocess.call(["sudo","kill",str(x)])

This does not kill the subprocess, but if I open terminal (let's say x is 1234), and type: sudo kill 1234 , it will kill the subprocess.

ProgrammerBret
  • 137
  • 2
  • 12

2 Answers2

0

Use x = str(return pid) and subprocess.call(["sudo","kill","-9",x]) and then try to grant root privileges. And, this allows to turn the process number to a string before calling the subprocess. Also, as I mentioned, use -9 (or -15 if you prefer using that). (Try to kill 1014 process too).

new Q Open Wid
  • 2,225
  • 2
  • 18
  • 34
  • Unfortuantely this did not work -- However I did a `ps -e --forest` to view the relationship and got this :`1009 ? 00:00:00 | \_ sudo 1014 ? 00:00:04 | \_ scrolling-text-` So there are two processes started by this -- It appears the **scrolling-text-** process is a child of the **sudo** process that is the one that returns x, which is 1009 – ProgrammerBret Feb 28 '20 at 02:11
  • **UPDATE** -- I find that this code does kill the 1009 process, but not the 1014 process -- so I need to kill BOTH as 1014 is dependent – ProgrammerBret Feb 28 '20 at 02:23
0

I found that the main process I identify with x = return.pid actually runs a child process which is the one I needed to kill, so from the parent process identified, we need to kill a child processes. The addition of "-P" includes child processes in this situation. The following command structure is what I needed:

subprocess.call(["sudo","pkill","-9","-P",x])
ProgrammerBret
  • 137
  • 2
  • 12