Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I will execute following command for reaping zombie

 /usr/bin/preap $(ps -ef | grep defunct | grep -v grep | awk '{ print $2 }' | xargs)

Is there any service impact of this approach ?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

If you reap a zombie before its parent, you lose whatever effect the reaping would have in the parent. This is obviously application-dependent.

There is very little reason to actively go and reap zombies. Some operating systems don't let you do it, short of manually ptracing the parent process and causing it to execute a waitpid system call. Solaris offers a preap utility, but the only case when you should use it is when a program is misbehaving and filling the process table with zombies.

share|improve this answer
    
it seems that application is misbehaving and zombie processes are only thing I am suspicious about. –  mibzer Nov 6 '12 at 8:40
    
@mibzer Zombie processes are probably a symptom, not the disease. Usually, zombie processes are left behind because the process that should reap them isn't doing what it should be doing (including waiting for its children). Reaping the zombies won't make it do whatever else it should be doing. –  Gilles Nov 6 '12 at 9:19

Your script might reap zombies too early preventing their parents to reap them and then causing unexpected behavior with them.

Assuming you have no way to fix the root cause of these zombie processes existence, I would only reap those that have been in the defunct state for a long enough period of time (eg. 1 minute) to avoid bypassing legitimate reaping:

for pid in $(ps -eo pid,s | nawk '$2 == "Z" {print $1 };sleep 60)
do
  preap $pid
done

By the way, | xargs is useless in your script as a new line is a valid argument separator.

share|improve this answer
    
What is “too early”? What are the downsides of reaping them too soon? How to determine “long enough”? –  Marco Nov 5 '12 at 12:20
    
That's a timing issue. If you happen to run your script just after a process dies, you need to let its parent process a chance to reap it and properly get its return status. –  jlliagre Nov 5 '12 at 12:23
1  
You would never know how long to wait. Better to let the parent do its job and fix it if it's not. –  Jim Paris Nov 5 '12 at 17:58
    
@Jim Paris: I agree the root cause should be fixed if possible but I assume the question means it is not possible for some reason. In any case, if the parent has not reaped a defunct child after say one minute, there is very little chance it will ever do it. –  jlliagre Nov 6 '12 at 7:04
    
Here is a counterexample that took no time to find: monit starts a program at regular intervals but doesn't reap them until the following interval starts, perhaps five minutes later. –  Jim Paris Nov 6 '12 at 15:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.