linuxshellzombie-process

How to kill defunct process on Linux by shell script


In my server machines, some process goes to the defunct state every day. It affects my CPU usage. I need to write a shell script to kill the defunct process id and parent id.

For example, when I run the command:

ps -ef|grep defunct.

it found many values. In that I need to kill only "[chrome] defunct" process.

sample entry:-

bitnami  12217 12111  0 Feb09 pts/3    00:00:00 [chrome] <defunct>

I need to kill this type of chrome entries. Can anyone suggest some samples to kill the entries?


Solution

  • Defunct processes do not go away until the parent process collects the corpse or the parent dies. When the parent process dies, the defunct processes are inherited by PID 1 (classically it is PID 1; it is some system process designated with the job), and PID 1 is designed to wait for dead bodies and remove them from the process table. So, strictly, the defunct processes only go away when their parent collects the corpse; when the original parent dies, the new parent collects the corpse so the defunct process goes away at last.

    So, either write the parent code so that it waits on its dead children, or kill the parent process.

    Note that defunct processes occupy very little resources - basically, a slot in the process table and the resource (timing) information that the parent can ask for. Having said that, last year I was working on a machine where there were 3 new defunct processes per minute, owned by a a system process other than PID 1, that were not being harvested. Things like ps took a long, long, long time when the number of defunct processes climbed into the hundreds of thousands. (The solution was to install the correct fix pack for the o/s.) They are not completely harmless, but a few are not a major problem.