1) Check current zombie process information
# Ps-ef | grep defunct | Grep-v grep | Wc-l
175
# Top | Head-2
top-15:05:54 up $ days, 23:49, 4 users, load average:0.66, 0.45, 0.39
tasks:829 Total, 1 running, 479 sleeping, 174 stopped, 175 zombie
# Ps-ef | grep defunct | Grep-v grep
2) Get the kill zombie process statement
# Ps-ef | grep defunct | Grep-v grep | awk ' {print ' kill-9 ' $2,$3} '
Execute the statement obtained above, using Semaphore 9, the number of zombie processes will be greatly reduced.
3) Check the current zombie process information in a moment
# Ps-ef | grep defunct | Grep-v grep | Wc-l
125
# Top | Head-2
top-15:29:26 up 98 days, MIN, 7 users, load average:0.27, 0.54, 0.56
tasks:632 Total, 1 running, 381 sleeping, stopped, zombie
found that the number of zombie processes decreased a little, but there are many ah.
4) Get the kill zombie process statement again
# Ps-ef | grep defunct | Grep-v grep | awk ' {print ' kill-18 "$ $} '
Execute the statement obtained above, this time using semaphore 18 to kill the parent process, the zombie process should be all gone.
5) Check the current zombie process information after a while
# Ps-ef | grep defunct | Grep-v grep | Wc-l
0
# Top | Head-2
top-15:39:46 up 98 days, MIN, 7 users, load average:5.46, 2.20, 1.12
tasks:134 Total, 1 running, 133 sleeping, 0 stopped, 0 zombie
6) Clear Zombie (zombie) Process principle
# kill-18 PPID
Ppid is its parent process, which tells the parent process that the child process has died, and withdraws the resources assigned to him. If not, then look at its parent process and no other child processes, if any, you may need to kill other child processes, that is, the sibling process.
The method is:
# kill-15 PID1 PID2
Pid1,pid2 is the other child process of the parent process of the zombie process.
Then kill the parent process:
# kill-15 PPID
In the UNIX System terminology, a process that has terminated,but whose parent have not yet waited for it, is called a zombie.
In a UNIX system, a process is over, but his parent process does not wait (call Wait/waitpid) him, then he will become a zombie process. In the fork ()/execve () procedure, the child process becomes a zombie process, assuming that the parent process is still present at the end of the child process, and the parent process fork () is not installed before the SIGCHLD signal handler call Waitpid () waits for the child process to end without explicitly ignoring the signal.
How to view zombie processes on Linux systems, how many zombie processes are counted?
#ps-ef | grep defunct
Or looking for a process with a status of Z, Z is the meaning of the zombie process representing zombie process.
In addition, using the top command to view a column is S, if the status of Z indicates that it is a zombie process.
tasks:95 Total, 1 running, 94 sleeping, 0 stopped, 0 zombie
The zombie process is also counted in the top command. or use the following command:
Ps-ef | grep defunct | Grep-v grep | Wc-l
How to kill the zombie process?
The general zombie process is hard to kill directly, but you can kill the zombie dad. After the parent process dies, the zombie process becomes the "orphan process", and the adoptive process init,init will always be responsible for cleaning up the zombie process. All of the zombie processes it produces also disappear.
PS-E-O Ppid,stat | grep Z | Cut-d ""-f2 | Xargs kill-9
Or
Kill-hup ' Ps-a-ostat,ppid | Grep-e ' ^[zz] ' | awk ' {print $} '
Of course you can write your own better shell script, welcome to share with you.
In addition, when the child process dies, the SIGCHLD signal is sent to the parent process, and after the parent process receives this signal, the waitpid () function is executed to corpse the child process. It is based on the principle that even if the parent process does not call wait, the kernel sends a SIGCHLD message to it, and while the default handling of it is ignored, you can set a handler function if you want to respond to this message.
How to avoid the zombie process?
Processing SIGCHLD signals is not a must. However, for some processes, in particular, server processes often generate child processes to process requests when requests arrive. If the parent process does not wait for the child process to end, the child process becomes a zombie process (zombie) and thus consumes system resources. If the parent process waits for the child process to end, it increases the burden on the parent process and affects the concurrency performance of the server process. The operation of the SIGCHLD signal can be easily set to Sig_ign under Linux.
Signal (sigchld,sig_ign);
This way, the kernel does not spawn a zombie process at the end of the child process. Unlike BSD4, the BSD4 must explicitly wait for the child process to end before releasing the zombie process
Or
With two fork (), and the immediate child process exits, the grandson process becomes the orphan process, and the INIT process will be responsible for clearing the orphan process.
How Linux kills Zombie processes