Today, we’ll be discussing Linux processes. Any program instance running on Linux is a process. For example, if you are using Firefox to reading this article, you may see it in your Linux terminal by typing out the “ps ax | grep firefox” command. I have provided a screenshot of Ubuntu here, so you can see how the output displays in real life.
As you can see, Firefox is running on my Ubuntu instance. Now, if we need to close this instance, we will use process termination. This is an advanced level of Linux, so I recommend you to learn the basics of Linux first.
Process in Linux
First, we need to understand that any program or application you run on your OS is called a process. Process management is a simple thing, and in today’s article, we are going to learn about process termination.
If you want to check the running process in your application, you can simply give a “ps” command.
$ ps PID TTY TIME CMD 3648 pts/0 00:00:00 bash 1493 pts/0 00:00:00 sleep 1300 pts/0 00:00:00 ps $
We have different ways to terminate the process. In this article, we are going to discuss basic process management and some advanced concepts like SIGTERM (signal 15), SIGINT, and other commands. Let’s start our article.
Process Termination in Linux
We already have different processes running on our systems. So, the question then is: What is process termination?
It is an operation in most OSs, mostly in UNIX-like OSs like Linux. Process termination, as the name suggests, occurs when the process is terminated: exit() is used by most UNIX-like OSs. So, why is a process terminated? This can happen for many reasons. For example, a process is terminated if it completes and leaves the processor. When the process leaves the processor, it releases all its processes.
Sometimes, the parent process may issue the command to terminate its child process. Failure and lack of memory are also common reasons for process termination.
1. Kill a Process by PID
We can manually kill a process using PID. PID is the process ID. If a process is hung or stuck and you need to kill it, you can do so, but you have to know the process ID or PID. If you notice, in a “ps” command, we have a column with PIDs. For killing a process, you have to issue the SIGTERM Signal. For this, in the Linux OS, we have to “kill” with the following syntax.
$ kill PID
If you are dealing with a process that is stuck/hanged, you can use -9 as well:
$ sudo kill -9 3212
[sudo] password for umair:
 Killed sleep 500
SIGTERM Signal is a signal. In the Linux OS, a signal is a communication method in different processes. Signal handlers interrupt the process and execute themselves. It is not necessary that every signal is for process termination. It depends on the signal and how the program is going to behave. If a process receives a signal, it may continue its normal execution or may not.
If we talk about SIGTERM and SIGQUIT, these two signals are for terminating processes on user request. The kill command that we just used to kill the process has SIGTERM Signal by default. You can visit the official Linux website documentation about different signals. There are also some forum threads on signals that may help you understand more.
Halloween is over! So why are we discussing zombies? What are zombie processes?
Simple—if a process job is finished executing, but it still remains in the process table, it is called a zombie process. Zombie processes are child processes, and their parent processes need to read the child exit() status. But parents might be skipped or stuck due to some reason and not able to listen to the zombie process.
Here, we come to our next point: dealing with such processes. These processes continue to exist until the parent process dies of system shutdown. An illustration of the process life cycle of zombie processes is given below.
When we call the exit() system call, all the memory and resources are deallocated, but the process is still available in the zombie list. The wait() system call is used to read the exit status of the zombie process. If a parent doesn’t call, the wait() system call will be the result of a resource leak. Another scenario is if the parent process is not in the process table anymore. In such cases, the zombie process availability is referred to as a system bug. If you want to kill the zombie processes, use the following command.
$ -s SIGCHLD PID
While using this command, please take care of the point that PID is the parent process ID. As you can see, we are sending SIDCHILD signals to the parent to kill the zombie process. This signal works as police and informs the parent process to clean zombie processes by using wait() calls. Zombie processes are not using any resources. So, if you have a few zombie processes, it might not harm your system, but if you have many zombie processes, it may occupy many PIDs and impact the starting of new processes.
I love Linux and have been using Linux for the last 20 years. It is an advanced OS, but it is not perfect (Linux lovers, don’t take offense. I think it is far better than Windows). Sometimes, a few applications start behaving strangely or consuming many system resources. In such cases, you need to restart the system, or you need to manage processes. Today’s article taught us about managing different processes and issues regarding processes.