- When Zombies Invade Linux: What are Zombie Processes and What to do about them
- What is a Zombie Process?
- How to spot a Zombie Process
- What is the difference between a Zombie and Orphaned Process?
- How to spot an Orphaned Process
- What to do about Zombie Processes?
- If the parent process is still active
- If the parent process is no longer active
- How to Find and Kill Zombie Processes on Linux Systems
- Выполняю установку, настройку, сопровождение серверов. Для уточнения деталей используйте форму обратной связи
- Watch Zombie Processes on Linux
- Resource Leak
- Detect Zombie Processes
- Resolve Issue
- Author
- Зомби процессы Linux
When Zombies Invade Linux: What are Zombie Processes and What to do about them
Zombies don’t just appear in scary movies anymore, sometimes they also appear on your Linux systems; but don’t fret they are mostly harmless.
What is a Zombie Process?
Before we get started I wanted to first cover what exactly a Zombie process is.
Linux and Unix both have the ability for a process to create a sub process otherwise known as a “Child Process”. Once a process creates a new sub process the first process then becomes a “Parent Process” as it has spawned a child process during its execution.
A Zombie or defunct process is a process that has finished its execution and is waiting for its Parent Process to read its exit status. Because the child process has finished, it is technically a “dead” process however since it is waiting for its parent there is still an entry in the process table. The zombie’s parent process does not necessarily need to be running for a zombie to appear, however it is most common to see a zombie process whose parent has died unexpectedly.
How to spot a Zombie Process
Zombie processes can be found easily with the ps command. Within the ps output there is a STAT column which will show the processes current status, a zombie process will have Z as the status. In addition to the STAT column zombies commonly have the words in the CMD column as well.
Example:
What is the difference between a Zombie and Orphaned Process?
Orphaned processes are very similar to Zombie processes; however there is one major difference. An Orphaned process is a child process that is still an active process whose parent has died. Unlike zombies the orphaned process will be reclaimed or adopted by the init process.
How to spot an Orphaned Process
Orphaned processes can be found easily with the ps command as well. Within the ps output there is a PPID column which will show the processes parent process id; a orphaned process will have the PPID of 1 which is the init process.
You may be thinking to yourself, how do I differentiate an Orphaned process from a Daemon process? Well in short, there is no difference. For all intents and purposes a daemon process is a orphaned process, however the exiting of the parent process is on purpose rather than by error.
Example:
What to do about Zombie Processes?
Before performing any activity to clean up zombie processes it is best to identify the root cause of the issue. Zombie processes do not indicate a normal state for your system, they may be benign for now however like real zombies they become more troublesome when they are in large numbers. They also indicate either a system issue or an application issue depending on the source of the processes.
The steps necessary to clean up zombie processes is complicated and very situational, below are a couple of high level answers that can guide you to a solution.
If the parent process is still active
If the parent process of the zombie or zombies is still active (not process id 1) than this is an indication that the parent process is stalled on a certain task and has not yet read the exit status of the child processes. At this point the resolution is extremely situational, you can use the strace command to attach to the parent process and troubleshoot from there.
You may also be able to make the parent process exit cleanly taking its zombie children with it by issuing the kill command. If you do run the kill command I suggest that you run a kill with the default signal -15 (SIGTERM) rather than using a -9 (SIGKILL) ; as SIGTERM will tell the parent process to exit cleanly which is more likely to read the exit status of the zombie children.
If the parent process is no longer active
If the parent process is no longer active than the clean up activity becomes a choice; at this point you can leave the zombie processes on your system, or you can simply reboot. A Zombie process whose parent is no longer active is not going to be cleaned up without rebooting the system. If the zombie processes are only in small numbers and not reoccurring or multiplying than it may be best to leave these processes be until the next reboot. If however they are multiplying or in a large number than this is an indication that there is a significant issue with your system.
Источник
How to Find and Kill Zombie Processes on Linux Systems
Posted by: Mohammed Semari | Published: February 27, 2018| Updated: February 27, 2018
In this mini post I’ll show you how to find and kill all zombie processes that may exist on your Linux Systems.
First, we need to define What is the Zombie process? and Why it exists?
A process is called a zombie process or defunct process if the process has been completed, but its PID and process entry remains in the Linux process table. A process is removed from the process table when the process is completed, and its parent process reads the completed process exit status by using the wait() system call. If a parent process fails to call wait() for whatever reason, its child process will be left in the process table, becoming a zombie.
How can we discover the existence of Zombie processes on Linux Systems?
Simply by using top command, this will show the status of all processes running on your Linux System. run the following command and notice it’s output:
From the above output, I’ve 4 zombie processes that completed their tasks and didn’t exit. It’s save to kill them, but first we need to list them.
How can we get the Zombie from process list…?
Its very simple. You can find out Zombie process with the following commands:
Now, we have the PID of the four zombie processes on our Linux System, let’s go and KILL THEM ALL using kill command, go ahead and run this command:
Hooray, we killed the zombie process, let’s double check, by running any of the above command:
Oh No, The Zombie Processes still exist on our Linux box, Kill command failed to kill them WHY?
Killing zombie processes is not obvious since zombie processes are already dead.
How can we KILL the Zombie processes?
Simply, we have to kill it’s parent process. If the parent exited, the child would be orphaned and re-parented to init, which would immediately perform the wait(). In other words, they should go away once the parent process is done.
We need to find the PPID “the parent process ID” of the zombie processes using the following command and search for the zombie process PID:
In our example, the parent of the zombie process is the monit monitoring with PID 4647. Now we kill the monit process “the parent” to kill all it’s zombie processes, run the following command:
Now, we check on the existence of ant zombie processes by using top and ps commands:
Hooray, zombie process count is ZERO,now ps command check
No results found from ps command. We totally killed all our zombie processes.
Finally, There is another way to kill the zombie processes without the need for pstree command to get the parent process ID, we can get the PPID and PID of the zombie process using ps command as follow:
In this example, we only have one zombie process, as shown by ps command, the parent process id of the zombie process is 3615.
To check the top command output, run
Indeed, we have one zombie process, now we kill it’s parent to kill it, run the following command:
Before we leave this post, there are more commands you use to get the number of zombie processes on your Linux:
To Know How many Zombie process running on your server…?
Finally, I hope this mini post helped you.
AND DO NOT FORGET TO START THE MONIT PROCESS AGAIN “The one in the first example” As Monit do a good job on monitoring and starting services.
Источник
Выполняю установку, настройку, сопровождение серверов. Для уточнения деталей используйте форму обратной связи
Что же это такое?
Это дочерний процесс в Unix-системе, завершивший своё выполнение, но ещё присутствующий в списке процессов операционной системы, чтобы дать родительскому процессу считать код завершения. Процесс при завершении освобождает все свои ресурсы (за исключением PID — идентификатора процесса) и становится «зомби» — пустой записью в таблице процессов, хранящей код завершения для родительского процесса.
Система уведомляет родительский процесс о завершении дочернего с помощью сигнала SIGCHLD. Предполагается, что после получения SIGCHLD он считает код возврата с помощью системного вызова wait(), после чего запись зомби будет удалена из списка процессов. Если родительский процесс игнорирует SIGCHLD (а он игнорируется по умолчанию), то зомби остаются до его завершения.
А теперь возникают вопросы: как же всё-таки их найти и убить? Найти их очень просто. Вот несколько вариантов:
1)
top | grep zombie
225 processes: 1 running, 222 sleeping, 2 zombie
2)
ps aux | grep -w Z
root 3994 0,0 0,0 0 0 ?? Z 13июн11 16:23,02
root 3995 0,0 0,0 0 0 ?? Z 13июн11 13:43,28
3)
ps -alx | awk ‘$10
Что касается «убийства», то их нельзя просто так убить. Самый правильный вариант — найти родительский процесс и перезапустить его. Некоторые могут посоветовать и перегрузиться, но это не выход.
Находим родительский процесс:
ps ajx | grep -w Z
root 3994 3992 3992 3992 0 Z ?? 16:23,02
root 3995 3992 3992 3992 0 Z ?? 13:43,28
3-я колонка как раз и показывает pid родительского процесса. Смотрим, что это за процесс:
ps auxww | grep 3992
root 3992 0,0 0,2 30664 9872 ?? Ss 13июн11 0:08,21 [exilog_agent] (perl5.12.3)
Собственно мы нашли виновника. Это exilog_agent. А дальше — либо просто прибиваем родительский процесс либо перезапускаем его:
#kill -9 3992
#top | grep zombie
#
Источник
Watch Zombie Processes on Linux
This post is older than a year. Consider some information might not be accurate anymore.
On Unix and Unix-like computer operating systems, a zombie process or defunct process is a process that has completed execution (via the exit system call) but still has an entry in the process table: it is a process in the “Terminated state”.
The term zombie process derives from the common definition of zombie — an undead person. In the term’s metaphor, the child process has “died” but has not yet been “reaped”. Also, unlike normal processes, the kill command has no effect on a zombie process.
Source: wikipedia.org, 2018-04-12
Resource Leak
After the zombie is removed, its process identifier (PID) and entry in the process table can then be reused. However, if a parent fails to call wait, the zombie will be left in the process table, causing a resource leak.
As with other resource leaks, the presence of a few zombies is not worrisome in itself, but may indicate a problem that would grow serious under heavier loads. Since there is no memory allocated to zombie processes – the only system memory usage is for the process table entry itself – the primary concern with many zombies is not running out of memory, but rather running out of process table entries, concretely process ID numbers.
Source: wikipedia.org, 2018-04-12
The number of processes that an individual can run can be checked with ulimit :
Check user process limit
Example check all limits
Example check user process limit
Detect Zombie Processes
How can you detect Zombies? Zombies can be identified in the output from the Unix ps command by the presence of a “Z” in the “STAT” column.
You can also use top . Starting top -H will show the amount of threads, instead of processes.
The amount of zombies should be monitored. On another server:
To check all your servers with Ansible:
The sed 1d command ignores the line of output. Since it is a header, we do not count them.
Resolve Issue
If the parent process of the zombie or zombies is still active (not process id 1) than this is an indication that the parent process is stalled on a certain task and has not yet read the exit status of the child processes. At this point the resolution is extremely situational, you can use the strace command to attach to the parent process and troubleshoot from there.
You may also be able to make the parent process exit cleanly taking its zombie children by gracefully stop or restart the process.
Example check parent process with pstree
If the parent process is no longer active than the clean up activity becomes a choice; at this point you can leave the zombie processes on your system, or you can simply reboot. A Zombie process whose parent is no longer active is not going to be cleaned up without rebooting the system. If the zombie processes are only in small numbers and not reoccurring or multiplying than it may be best to leave these processes be until the next reboot. If however they are multiplying or in a large number than this is an indication that there is a significant issue with your system.
Author
Tan-Vinh Nguyen
The happiest people don’t have the best of everything, they just make the best of everything they have.
Birth, Development, in Progress .
Источник
Зомби процессы Linux
Каждая программа, которая выполняется в Linux, — это системный процесс, у которого есть свой идентификатор. Каждый процесс может запускать дочерние процессы с помощью функции fork. Такие процессы остаются под контролем родительского процесса и не могут быть завершены без его ведома. Если один из дочерних процессов всё же завершился, а его родительский процесс не смог получить об этом информацию, то такой дочерний процесс становится зомби.
Зомби процессы Linux не выполняются и убить их нельзя, даже с помощью sigkill, они продолжают висеть в памяти, пока не будет завершён их родительский процесс.
Посмотреть такие процессы можно с помощью утилиты ps, здесь они отмечаются как defunct:
ps aux | grep defunct
Если вы попытаетесь убить такой процесс с помощью сигнала KILL, то ничего не выйдет:
Чтобы его завершить, нужно найти «родителя» этого процесса. Для этого используйте команду:
ps -xal | grep defunct
Здесь идентификатор родительского процесса находится в четвёртой колонке (PPID). Теперь мы можем послать ему сигнал завершения, и такого процесса в системе больше не будет:
Для большего удобства вы можете использовать утилиты top или htop, но принцип их действия будет аналогичным, поэтому я не буду здесь его рассматривать. Теперь вы знаете, что делать, если в вашей системе появились зомби процессы Linux.
Источник