Linux killing zombie processes

How To Kill Zombie Processes on Linux

Killing Zombies!

Also known as “defunct” or “dead” process – In simple words, a Zombie process is one that is dead but is present in the system’s process table. Ideally, it should have been cleaned from the process table once it completed its job/execution but for some reason, its parent process didn’t clean it up properly after the execution.

In a just (Linux) world, a process notifies its parent process once it has completed its execution and has exited. Then the parent process would remove the process from process table. At this step, if the parent process is unable to read the process status from its child (the completed process), it won’t be able to remove the process from memory and thus the process being dead still continues to exist in the process table – hence, called a Zombie!

In order to kill a Zombie process, we need to identify it first. The following command can be used to find zombie processes:

$ ps aux | egrep «Z|defunct»

Z in the STAT column and/or [defunct] in the last (COMMAND) column of the output would identify a Zombie process.

Now practically you can’t kill a Zombie because it is already dead! What can be done is to notify its parent process explicitly so that it can retry to read the child (dead) process’s status and eventually clean them from the process table. This can be done by sending a SIGCHLD signal to the parent process. The following command can be used to find the parent process ID (PID):

Once you have the Zombie’s parent process ID, you can use the following command to send a SIGCHLD signal to the parent process:

However, if this does not help clearing out the Zombie process, you will have to kill or restart its parent process OR in case of a huge surge in Zombie processes causing or heading towards system outage, you will have no choice but to go for a system reboot. The following command can be used to kill its parent process:

Note that killing a parent process will affect all of its child processes, so a quick double check will be helpful to be safe. Alternatively, if few lying zombie processes are not consuming much CPU/Memory, it’s better to kill the parent process or reboot the system in the next scheduled system maintenance.

Nawaz is a Linux CLI enthusiast and works as System Development Engineer at Amazon. Having a knack for Automation, he believes that «every manual and repetitive task should be done meticulously to such an extent that its automation becomes a necessity!» Nawaz can be reached via LinkedIn and email.

Источник

Killing zombie processes on Linux using kill command

Z ombie process is an inactive computer process. On Unix, including Linux operating systems, a zombie process or defunct process is a process that has completed execution but still has an entry in the process table, allowing the process that started it to read its exit status. In the term’s colorful metaphor, the child process has died but has not yet been reaped. Let us see how to list and kill zombie processes on Linux command line options.

How do I find out zombie processes on Linux?

Use the top command or ps command along with grep command/egrep command:
# top
Try the following commands to find zombie processes as root user:
# ps aux | awk ‘< print $8 " " $2 >‘ | grep -w Z
Here is what we see

To get more human readable outputs try:
# ps aux | egrep «Z|defunct» | grep -v ‘grep’

How to find zombie process on Linux

How do I kill zombie process or processes on Linux?

You cannot kill zombies, as they are already dead. But if you have too many zombies then kill parent process or restart service. For example, you can kill zombie process using PID obtained from any one of the above commands. First, get parent PID for child PID called 1313
# ps -o ppid=1313
Next, kill zombie process having parent PID 4104:
# kill -s SIGCHLD 4104

Читайте также:  Создание внутренней сети windows 10

Killing zombie processes for good

If above command failed, try the following command to kill its parent process:
# kill -9 4104
Please note that kill -9 does not guarantee to kill a zombie process. In extreme, condition you may have to reboot your Linux box by typing the shutdow command/reboot command:
$ sudo reboot
## OR ##
$ sudo shutdown -r now

How do I automate zombie process killing?

Write a shell script and schedule as a cron job to kill zombie process.

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Conclusion

You learned how to list and kill zombie processes on Linux operating systems. See kill command man page online or by typing the following command:
$ man kill
$ man ps

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

My friend is using something as follow (he calls it as linux zombies kill script.. LOL)

for each in `ps jauxww | grep Z | grep -v PID | awk ‘’`; do for every in `ps auxw | grep $each | grep cron | awk ‘’`; do kill -9 $every; done; done

Hope it will be useful to someone.

awk: cmd. line:1: . awk: cmd. line:1: ^ syntax error
awk: cmd. line:2: (END OF FILE)
awk: cmd. line:2: syntax error
ERROR: Conflicting format options.

Just copy and paste the code. I have fixed the css code problem.

grepping for the “word” Z won’t show Zombies that are also session leaders (such as Xorg) which show up as Zs

recommend ending with “| grep Z”

You should read the Wikipedia article more closely. There is no point sending a SIGKILL to a zombie process.

$ cat makezombie.c
#include
#include
#include
#include

int main(int argc, char *argv[])
<
pid_t child;

for (;;)
<
child = fork();
if (0 == child)
<
/* Whaddya know, I am the walrus^Wchild. Announce myself and
* exit immediately, making myself a zombie.
*/
fprintf(stdout, “Child PID is %ldn”, (signed long int)getpid());
exit(0);
>
else if (child > 0)
<
/* I’m the parent. Loop without
* reaping the child.
*/
for (;;)
<
sleep(1);
>
>
else
<
/* fork() failed. Wait and try again. */
perror(“fork”);
sleep(1);
>
>
>
$ cc makezombie.c -o makezombie
$ ls -l makezombie
-rwxr-x— 1 youngman eng 9871 Mar 27 16:52 makezombie
$ ps
PID TTY TIME CMD
1769 pts/2 00:00:00 bash
2348 pts/2 00:00:00 ps
$ ./makezombie &
[1] 2351
$ Child PID is 2352
ps
PID TTY TIME CMD
1769 pts/2 00:00:00 bash
2351 pts/2 00:00:00 makezombie
2352 pts/2 00:00:00 makezombie
2353 pts/2 00:00:00 ps
$ kill -9 2352; echo $?
0
$ ps
PID TTY TIME CMD
1769 pts/2 00:00:00 bash
2351 pts/2 00:00:00 makezombie
2352 pts/2 00:00:00 makezombie
2356 pts/2 00:00:00 ps
$ kill 2351
$ ps
PID TTY TIME CMD
1769 pts/2 00:00:00 bash
2364 pts/2 00:00:00 ps
[1]+ Terminated ./makezombie
$

Plz tell me !
The difference between a thread and a process.
Plz give a detailed explanation.
I hope you will not let me down.
Thank you.

The first script has major issues with it. This is the one that I use

for each in `ps -ef | grep ” | grep -v PID | awk ‘< print $3 >’`; do for every in `ps -ef | grep $each | grep -v cron | awk ‘< print $2 >’`; do kill -9 $every; done; done

thanks.
I Got my ans of zombie Process.

The thread is some thing like background job for the process smiler to project count from 1 till 100 and the main purpose of the project is get each number value.
eg in our daily life is some programs can’t do any thing until it’s main job done like some sort of Anti Virus’s when you tried to do another job the program dosen’t respond to any command in the main time i’s in the same process but it take her own mem space 😀
i hope i was any useful for you 😀

Updated and tested with HP-UX 11i, Solaris 10, Debian, RHEL4 and SUSE 10

Quick ‘n dirty hack:

ps ax | awk ‘< if ($NF == “”) print $1 >’ | xargs kill -9

I guess the html is stripped, hope at least one works, I mean $NF == defunct between “less than” and “greater than”:

ps ax | awk ‘< if ($NF == “”) print $1 >’ | xargs kill -9

You need to send the parent process a SIGCHLD or kill it

ps -eo pid,ppid,user,args,stat –sort stat | grep Z | awk ‘< print $2 >’ | sort -u

gives you the parent process id(s)

kill -s SIGCHLD
or
kill

First things first, I am amazed at the amount of time so many people have put into dealing with such a non-issue. Zombie processes are dead, they consume nearly zero system resources. The only time you should be concerned is if you have a large number of them and said number is continuing to grow, in which case getting rid of the zombies is not your problem. You need to find out why you have them in the first place. Removing them is like taking a cold tablet, you are deling with the symptom, not the real problem.

That said, you should never, ever be using ‘kill -9’, that is an absolute last-ditch resort for dealing with a system that is almost non-functional or in some other critical state. Having a few zombie processes tying up nearly no resources at all is not a critical state, and using kill -9 is like chasing a mouse with a heavy hammer; you’re not likely to get the mouse, but you may do extensive damage to everything around you in the process.

So, what to do? find out what is spawning these processes, and why it is not shutting them down when they exit. See if there is a patch that fixes the issue. Make a report to the maintainers of the package. Restart the parent process. Or to reiterate, find out the cause of the problem and fix that.

you’re not likely to get the mouse, but you may do extensive damage to everything around you in the process.

if I use kill -9 to erase zombies from servers what kind of problems might I cause ?
can you give me a hint
thanks

ps -ef | grep defunct | awk ‘< print $3 >’ | xargs kill -9

Источник

How to Kill Zombie Processes in Ubuntu 18.04 LTS

A zombie or a defunct process in Linux is a process that has been completed, but its entry still remains in the process table due to lack of correspondence between the parent and child processes. Usually, a parent process keeps a check on the status of its child processes through the wait() function. When the child process has finished, the wait function signals the parent to completely exit the process from the memory. However, if the parent fails to call the wait function for any of its children, the child process remains alive in the system as a dead or zombie process. These zombie processes might accumulate, in large numbers, on your system and affect its performance. In that case, you might have to kill these zombies manually through the ways and commands described in this tutorial.

Viewing Zombie Processes

You can have a check on your system performance by viewing the various processes running on your system, including the efficiency altering zombie processes. Ubuntu allows you to view these processes in the following manner:

  • Through the Graphical User Interface
  • Through the Command Line

Through the GUI

In order to graphically view any zombie processes running on your system, open the System Monitor utility through your Ubuntu Dash. In the following screenshot of my System Monitor, you can view that there are two zombies running on my system. It is also possible that the number of zombie processes on your system might be less or more than the ones running on mine.

Through the Command Line

The top command displays a detailed view of the processes running on your system along with the memory and CPU resources they are using. It also gives you information about any zombie processes running on your system. Open the Terminal by pressing Ctrl+Alt+T and then type top. I got the following output after running this command.

You can see in the second line that there is 1 zombie process running on my system.

If you want further details about the zombie process, use the following command:

This command will give you the state, parentID, the process ID, the program that is running the zombie process(a dummy program by the name ‘zombie’ on my system). The defunct flag tells you that this is a dead, zombie process.

Killing a Zombie-Process

First, let us understand how zombie processes are a threat to our system’s performance. It is important to learn that zombies are dead and mostly completed processes that do not take memory or CPU resources. However, each of these processes has a unique process ID assigned to them which comes from a limited pool of PIDs reserved for your processor. If a large number of zombies gather, they will eat up most part of the PID pool and the new processes will not be able to launch due to lack of a process ID. Advertisement

A small number of defunct programs occupying your system is not a big threat but that means that their parent programs have not been able to call them off due to a bug or a missing wait() function.

When a parent process has not been able to call the wait() function automatically, we need to manually signal the parent process to run the wait function on all its children so the ones with a complete state can be called back. We can do this by running the SIGCHLD command. When that doesn’t work, we can manually kill the parent process so that all its zombie children are also killed, freeing the process IDs for the new processes.

You can kill the zombie processes through the following ways:

  • Through the Graphical User Interface
  • Through the Command Line

Through the GUI

You can kill a zombie process graphically through the System Monitor Utility as follows:

  1. Open the System Monitor utility through Ubuntu Dash.
  2. Search for the term Zombie through the Search button.
  3. Select the zombie process, right-click and then select Kill from the menu.

The zombie process will be killed from your system.

Through the Command Line

After you know that there any zombie processes running on your system through the top command, view the details of the processes.

The usual way is to use the following command that signals the zombie’s parent process to kill the command.

This command may not work in a few cases as not all parent processes are programmed properly in order check upon the child processes. In that case, you can kill the parent process through the following command:

When you have killed all the zombie processes through this way and run the top command, you will be able to see that there are no zombie processes running on your system anymore:

After working along with this tutorial, you can optimize your operating system by looking for any zombie processes on your system and killing them manually through the command line or the graphical user interface. This act will free up process IDs for the new processes that you want to run on your system.

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn

Источник

Читайте также:  Inventor mit windows 10
Оцените статью