Bash: identifying and killing a zombie child processes

Killing a defunct zombie process means identifying and killing the parent process.  In this article, I will show how you can force a zombie process so that you can practice removing it.

A simple way of forcing a zombie process is to start a process that has a short sleep in the background,  immediately invoking a child process that last longer.  The command below will create a zombie child process for a minute.

# use subshell, otherwise kill will end the session
$(sleep 1 & exec /bin/sleep 60)

During this 60 seconds, you can use ps to show the defunct zombie child process and its parent process id.  top will also report the zombie process.

# shows defunct zombie child process 
$ ps ux | awk '{if($8=="Z+") print}'
fabian 1209381 0.0 0.0 0 0 pts/1 Z+ 08:01 0:00 [sleep] <defunct>

# shows child and then parent process id
$ ps -A -ostat,pid,ppid | grep -e '[zZ]'
Z+ 1209381 1209380

# kill parent process
$ kill -9 1209380

REFERENCES

sobyte.net, how to find and kill zombie processes

linux.stackexchange, creating zombie in bash

stackoverflow, zombie process in c

NOTES

another way to find the parent process of a child PID

ps -o ppid= -p <child_id>