Zombie processes in Linux are sometimes also referred to as defunct or dead processes. They’re processes that have completed their execution, but their entries are not removed from the process table.
What are different Process States?
Linux maintains a process table of all the processes running, along with their states. Let’s briefly overview the various process states:
Running (R): These processes are currently running or runnable.
Waiting (S/D): These are the processes that are waiting for an event or a resource. The wait can either be an interruptible sleep (S) or an uninterruptible sleep (D).
Stopped (T): We can stop a Linux process by sending an appropriate signal.
Zombie (Z): When a process finishes its task, it releases the system resources it was using and cleans up its memory. However, its entry from the process table is not removed, and its status is set as EXIT_ZOMBIE.
How Zombie Processes gets created?
When a process completes its job, the Linux kernel notifies the exiting process’s parent by sending the SIGCHLD signal. The parent then executes the wait() system call to read the status of the child process and reads its exit code.
This also cleans up the entry of the child process from the process table, and hence, the process finishes.
However, if a parent process isn’t programmed to execute the wait() system call on the creation of the child process, proper cleanup doesn’t happen.
In such cases, the parent process cant monitor the state changes of the child processes, and eventually, it ignores the SIGCHLD signal. This causes the zombie state of the finished process to stay in the process table, and hence it appears in the process list as a zombie process.
Another case of interest is when a parent process is unable to handle or receive the SIGCHLD signal from the child process. Such cases also lead to zombie creation.
How to Identify Zombie Processes?
We can identify the list of zombies using the ps command:
As observed from the output, the Z in the STAT column or zombie or <defunct> pattern from the output of the ps command can be used to identify the zombies.
$ ps aux | egrep "Z|defunct"
We can find out Z process state using the awk command as well:
$ ps ux | awk '{if($8=="Z") print}'
How to clean a Zombie Process?
You can’t really kill a zombie process since it’s already dead. However, there are a few workarounds we can use to clean up a zombie process.
Method 1 -
Using SIGCHLD Signal -
We can manually send the SIGCHLD signal to d parent of a zombie process. Consequently, it'll intimate d parent to trigger d wait() system call, which will clean up d defunct child process from d process table.
Let’s find d parent id of our defunct process:
$ ps -A -ostat,pid,ppid | grep -e '[zZ]'
Next, let’s send the SIGCHLD signal to d parent process using d kill cmd:
$ kill -s SIGCHLD 103
However, it isn’t really guaranteed that sending the SIGCHLD signal to the parent will kill a zombie process. It works only in cases where parent processes can handle the SIGCHLD signals.
Method 2 -
Killing d Parent Process -
If d method discussed in d previous section is unable to clear d defunct process, we should consider killing its parent process:
$ kill -9 103
Here, 103 is the parent id of our defunct process with PID 108.
However, killing a parent can affect all its childs. Hence, we shud exercise extra caution & must identify d impact before killing a parent process.
If der are a lot of Z processes,or if d parent of d Z process is d init process (with pid=1),we can also consider a system reboot.
Retweet if you liked the thread. Follow me for more such content.
PC: baeldung
• • •
Missing some Tweet in this thread? You can try to
force a refresh
What is systemd and why should Linux users care about it?
Everything about "systemd" !!
A Mega Thread 👇
What is systemd ?
systemd is the glue that holds Linux systems together. systemd is a collection of building blocks, which handle services, processes, logging, network connectivity and even authentication.
systemd handles the boot process for Linux systems. As an init implementation, it has a PID of 1 like other init systems, such as System V, Upstart.
It was designed as a replacement for SystemV and LSB-style startup scrips, which were prevalent since 1980s.
Every Linux Admin or DevOps Engineer should know what happens when a Linux system boots. It's a very popular Interview Question as well.
Every time you power on your Linux PC, it goes through a series of stages before finally displaying a login screen that prompts for your username or password.
There are 3 high level stages of a typical Linux boot process.
Everything you need to know about Virtualization, VMs , Containers, Pods, Clusters ..
A Mega Thread 👇
What is Virtualization?
Virtualization is the act of dividing shared computational resources: CPU, RAM, Disk, and Networking into isolated resources that are unaware of the original shared scope.
What is a virtual machine?
A VM is a virtual env that functions as a virtual computer system with its own CPU, memory, nw interface, & storage, created on a physical hw system (located off- or on-prem).
It uses sw instead of a physical computer to run programs & deploy apps.
traceroute tracks the route packets take across an IP network on their way to a given host.
It assists you in troubleshooting nw connectivity issues from your Destination to a Remote destination by using echo packets (ICMP) to visually trace the route.
The syntax -
The cmd traceroute <x> (x here being an IP or hostname) is d most basic version & it will begin to send packets to d designated target. This result will allow u to trace d path of d packets sent from ur machine to each of d systems b/n u & ur desired destination.
Cybersecurity is a way of protecting the network, computers, and other electronic gadgets from cybercriminals. The Malicious attackers might delete, modify or leak confidential information posing a huge threat to a business or an individual.