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 ?


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.

  • 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)
  preap $pid

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

  • 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
  • 2
    if you have zombie processes who's parent pid is '1' (init), those you can reap. If a zombie process still reports it's actual parent as it's PPID, wait it out. – Tim Kennedy Nov 13 '12 at 5:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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