I am using subprocess in python to start a program.When it starts it saves the pid of the process created in a database.After some time and if a trigger happens it needs to stop this process and start a new one. The problem is that when I use subprocess.Popen().pid it returns the pid of a zombie process (defunct) and not the real process I need to stop.I cannot use the terminate command since the start process command and the kill command happens in different scripts.So I need a way to get the real pid of the process I am starting and not the pid of the zombie process.
1 Answers
A zombie process is a dead process, which its data is still kept by the os to be returned to the calling process (exit value etc). You can't kill a zombie process, since it's already dead. There are 2 ways to get rid of it; either Popen.wait()
for the subprocess from its parent before the parent exits or in the background using Popen.poll()
if the parent process should keep running, OR kill all parent processes until the zombie's parent is 1 init
, and init will reap it automatically. You should then update your DB that your process exited, since linux may give the same pid to a new unrelated process (you may end up killing a process you didn't intend to kill).
If all you need is to stop the process, then don't worry - it is already stopped. However, you do need to worry about too many zombies in the system (some say that even 1 is too many), since linux will issue a pid for each process, and having too many of these may prevent you from being able to run new processes.
There's a hack to force parent processes to reap their children here.

- 1
- 1

- 6,747
- 2
- 20
- 29
-
I just want to get the correct pid of the process and not the pid of the zombie.I dont think I can accept this answer.Thank you anyway. – vkefallinos Oct 18 '13 at 17:14
-
You have the correct pid. There is no other pid. The process has started with a pid, and exited. The pid does not change. You got the correct pid, but there's nothing you can do to reap it except the above. – micromoses Oct 18 '13 at 17:17
-
Perhaps [this](https://www.google.co.il/#q=what+is+a+zombie+process) will help. – micromoses Oct 18 '13 at 17:19