Question

In my Python script, I first launch a subprocess by subprocess.Popen(). Then later on, I want to kill that subprocess by kill -9 Pid.

What I found is that after the kill is executed, the subprocess is "stopped" because the GUI window of that process disappeared immediately. But when I perform a "ps aux" right after the kill, the same process (with same pid) is still shown in the result. The difference is the command of the process is included in a pair of () like below:

root 30506 0.0 0.0 0 0 s000 Z+ 6:13PM 0:00.00 (sample process)

This breaks my process detect logical since the dead process still can be found by ps.

Anyone know why this is happening?

Thanks!

Was it helpful?

Solution

From the manual page of ps:

Z Defunct ("zombie") process, terminated but not reaped by its parent.

That means that the parent didn't do a waitpid() for the child that died.

Apart from waitpid(), you can avoid that by using a double fork when executing the child.

OTHER TIPS

I think -9 signal lets the process to try to handle kill and spend some time housekeeping. You can try just kill the process without signal.

Edit: oh, its actually -15 signal, that lets process die gracefully. never mind.

Zombie processes are actually just an entry in the process table. They do not run, they don't consume memory; the entry just stays because the parent hasn't checked their exit code. You can either do a double fork as Gonzalo suggests, or you can filter out all ps lines with a Z in the S column.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top