Hi,

Xianwen Chen wrote on Thu, Feb 13, 2020 at 08:10:17PM +0000:

> I am not able to kill a python process.
> $ pgrep python
> showed a PID of 8926
> However, I am not able to kill the process.
> $ kill -9 8926
> # kill -9 8926
> Running as root did not help.

Sounds like a zombie.  Seriously, i'm not joking.

> How can I kill this process?

You can't, a zombie is already dead.

Here is how i reproduced:

   $ sh
   $ echo $$
  39747
   $ python3
  Python 3.7.6 (default, Jan  1 2020, 13:51:25) 
  [Clang 8.0.1 (tags/RELEASE_801/final)] on openbsd6
  Type "help", "copyright", "credits" or "license" for more information.
  >>>

Now, from a different terminal:

   $ kill -STOP 39747  # Block the reaper. (Seriously, i'm not joking.)
   $ pgrep python    
 61181
   $ kill 61181
   $ ps axu -p 61181 
  USER       PID %CPU %MEM VSZ RSS TT STAT STARTED    TIME COMMAND
  schwarze 61181  0.0  0.0   0   0 pb Z    -       0:00.00 (python3.7)

The "STAT: Z" tells you it's a zombie.  It is already dead, but
still haunting the operating system.  You cannot kill(2) it; or
more precisely, killing it again won't make it any more dead than
it already is.

(Unless you pierce its heart with a woodden stick.  Sorry, now i was
joking.)

Just let the parent process reap it.  A well-behaved parent process
will do so by wait(2)ing for its children to die.  My earlier -STOP
signal prevented the parent shell from doing its job, so lets allow
that shell to get back to work:

   $ kill -CONT 39747
   $ ps axu -p 61181  
  USER       PID %CPU %MEM VSZ RSS TT STAT STARTED    TIME COMMAND
   $ 

So, now we got rid of the zombie by allowing the parent shell to
reap it.

If the parent process is ill-mannered and does not wait(2) on its
children, just kill the parent.  In that case, the parent deserves
the punishment.

Read the following manual page to understand why dying processes
don't vanish instantly but instead linger around until wait(2)ed
for by their parent:

  https://man.openbsd.org/wait.2

And see this manual page for what parents can do if they do not
need information about dying children:

  https://man.openbsd.org/sigaction.2#SA_NOCLDWAIT

Either way, don't create children if you aren't prepared to handle
the responsibility that comes with them - or you might end up being
haunted by zombies.

Yours,
  Ingo

Reply via email to