0

What I want? Create a script that starts and kill a communication protocol

What I have? I have a python script that opens a shell script, and this shell script initialize the protocol. When I kill the parent process, everything goes fine (but in the final project, the parent process will have to stay alive), but when I kill the subprocess, it become a zombie function, and my protocol keep running.

Problems I believe can be: I'm "killing" the shell script (not the protocol, that's what I want)

The line I start the shell script:

`protocolProcess = subprocess.Popen(["sh", arquivo], cwd = localDoArquivo) #inicia o protocolo`

protocolProcessPID = protocolProcess.pid #armazena o pid do protocolProcess

The line I kill the shell script: os.kill(protocolPID, signal.SIGTERM)

Well, that's it! If anyone can help me, I'll be very grateful

Fernando Santos
  • 392
  • 4
  • 19
  • related: [How to terminate a python subprocess launched with shell=True](http://stackoverflow.com/q/4789837/4279) – jfs Apr 30 '16 at 17:02

1 Answers1

1

Zombie processes are processes that have not yet been reaped by the parent process.

The parent process will hold onto those process handlers until the end of time, or until it reads the process exit status, or itself is killed.

It sounds like the parent process needs to have a better handle on how it spawns and reaps it's children. Simply killing a child process is not enough to free a zombie process.

sverasch
  • 102
  • 2
  • Is there a right and recommended way to start and finish it?? – Fernando Santos Apr 30 '16 at 07:18
  • @FernandoSantos: don't worry about zombies unless there are many of them. See [What is for a process and why it doesn't get killed?](http://askubuntu.com/q/201303/3712) – jfs Apr 30 '16 at 17:03
  • You should use the terminate() function to kill the child. And use poll until the process has died. Once the process has died and you've read the return code it should consume the zombie process. – sverasch Apr 30 '16 at 18:31
  • The function terminate() will kill my child and all processes created by it?? Because if I kill my child process, my protocol (which is started using a Python script) will get defunct and vice versa – Fernando Santos Apr 30 '16 at 19:29
  • I think I know what you're getting at. You'll want to either [fork](https://docs.python.org/2/library/os.html#os.fork), or [daemonize](http://stackoverflow.com/questions/473620/how-do-you-create-a-daemon-in-python) your process. Forking allows the process to change it's parent to 0 (the system) and continue to run. – sverasch Apr 30 '16 at 20:16