Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - subprocess.Popen().pid return the pid of the parent script

I am trying to run a Python script from another Python script, and getting its pid so I can kill it later.

I tried subprocess.Popen() with argument shell=True', but thepidattribute returns thepid` of the parent script, so when I try to kill the subprocess, it kills the parent.

Here is my code:

proc = subprocess.Popen(" python ./script.py", shell=True)
pid_ = proc.pid
.
.
.
# later in my code

os.system('kill -9 %s'%pid_)

#IT KILLS THE PARENT :(
like image 700
farhawa Avatar asked Jun 25 '15 01:06

farhawa


People also ask

What does Popen return Python?

Description. Python method popen() opens a pipe to or from command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'.

What does Popen shell do?

DESCRIPTION top. The popen() function opens a process by creating a pipe, forking, and invoking the shell. Since a pipe is by definition unidirectional, the type argument may specify only reading or writing, not both; the resulting stream is correspondingly read- only or write-only.

What does subprocess Check_call return?

subprocess. check_call() gets the final return value from the script, and 0 generally means "the script completed successfully".

What is difference between subprocess Popen and call?

Popen is more general than subprocess. call . Popen doesn't block, allowing you to interact with the process while it's running, or continue with other things in your Python program. The call to Popen returns a Popen object.


1 Answers

shell=True starts a new shell process. proc.pid is the pid of that shell process. kill -9 kills the shell process making the grandchild python process into an orphan.

If the grandchild python script can spawn its own child processes and you want to kill the whole process tree then see How to terminate a python subprocess launched with shell=True:

#!/usr/bin/env python
import os
import signal
import subprocess

proc = subprocess.Popen("python script.py", shell=True, preexec_fn=os.setsid) 
# ...
os.killpg(proc.pid, signal.SIGTERM)

If script.py does not spawn any processes then use @icktoofay suggestion: drop shell=True, use a list argument, and call proc.terminate() or proc.kill() -- the latter always works eventually:

#!/usr/bin/env python
import subprocess

proc = subprocess.Popen(["python", "script.py"]) 
# ...
proc.terminate()

If you want to run your parent script from a different directory; you might need get_script_dir() function.

Consider importing the python module and running its functions, using its object (perhaps via multiprocessing) instead of running it as a script. Here's code example that demonstrates get_script_dir() and multiprocessing usage.

like image 179
jfs Avatar answered Sep 18 '22 19:09

jfs