python - How to kill properly all processes launched by the application? -


we have big application written in python. wish cleanly kill processes have been launched our application when close or interrupt it. however, through python code (e.g. subprocess.popen or os.system), have command a launches subcommand b command b seems have no relation command a. father process of command b init process (with pid 1). how kill process b in python code (e.g. after ctrl+c)?

for example, have 2 shell scripts , 1 python script calls these shell scripts.

the first shell script mycommand1.sh is:

#!/bin/sh sleep 3600 & 

the second shell script mycommand2.sh is:

#!/bin/sh sleep 3600 

the python script myscript.py is:

import os import signal import subprocess  def launchcommands():      proc1 = subprocess.popen('./mycommand1.sh', stdout=subprocess.pipe, shell = true, preexec_fn=os.setsid)     print "the processus 1 has started"      proc2 = subprocess.popen('./mycommand2.sh', stdout=subprocess.pipe, shell = true, preexec_fn=os.setsid)     print "the processus 2 has started"      return proc1, proc2   if __name__ == '__main__':     proc1 = none     proc2 = none     try:         proc1, proc2 = launchcommands()         while true:             pass      except keyboardinterrupt:         print "ctrl+c received! ..."          print "trying kill processus 2"         os.killpg(os.getpgid(proc2.pid), signal.sigterm)         print "processus 2 has been killed"          print "trying kill processus 1"         os.killpg(os.getpgid(proc1.pid), signal.sigterm)         print "processus 1 has been killed" 

then execute our python script , interrupt after 3 seconds ctrl+c. here output after interruption.

$> python myscript.py processus 1 has started processus 2 has started ^cctrl+c received! ... trying kill processus 2 processus 2 has been killed trying kill processus 1 traceback (most recent call last): file "myscript.py", line 33, in <module>   os.killpg(os.getpgid(proc1.pid), signal.sigterm) oserror: [errno 3] no such process 

we display "sleep" processes while , after execution of python script (of course, there no sleep process before execution of python script).

while running myscript.py command:

$> ps -ef | grep sleep   501 50700     1   0  2:21   ??         0:00.00 sleep 3600   501 50701 50699   0  2:21   ??         0:00.00 sleep 3600   501 50703   410   0  2:21   ttys002    0:00.00 grep sleep 

after interrupting program ctrl + c:

$> ps -ef | grep sleep   501 50700     1   0  2:21   ??         0:00.00 sleep 3600   501 51025   410   0  2:23   ttys002    0:00.00 grep sleep 

as can see, process 1 try kill not exist anymore , 1 process sleep father pid 1 exists. process sleep comes execution of script mycommand1.sh. how kill process sleep in script myscript.py (of course, not wish kill sleep process)?

try rewrite mycommand1.sh

#!/bin/sh foo(){     cnt=0     while [ $cnt -le 100 ];         cnt=$((cnt+1))         echo $0 $cnt         sleep 1     done }  foo & 

and mycommand2.sh

#!/bin/sh foo(){     cnt=0     while [ $cnt -le 100 ];         cnt=$((cnt+1))         echo $0 $cnt         sleep 1     done }  foo 

remove stdout=subprocess.pipe myscript.py , check it.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -