How to kill linux defunct process by shell script

Mohan Shanmugam picture Mohan Shanmugam · Feb 10, 2012 · Viewed 12.6k times · Source

In my server machines some process goes to defunct state for every day. it affects my CPU usage. 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.

found may 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 any one suggest some samples to kill the entries

Answer

Jonathan Leffler picture Jonathan Leffler · Feb 10, 2012

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.