When you launch a program or application, you hope that everything runs smoothly with no glitches, right? However, that’s not always the case. Sometimes, you may get a case where your program gets stuck and refuses to close.
It is best to kill that process using its PID when that happens. Luckily, there is a command that you can use to kill a process with its PID in Linux. Stick around, and you will learn about this command and how to use it. Let’s begin!
How to Check the PID of a Running Process
Before you can kill a process in Linux, you must begin by knowing its PID. Whenever you open any program or process, a PID is created. Your system relies on the PID to know which process is running and to facilitate a communication between processes.
So, to kill a command, you must first know how to get its PID. There are three main options that you can use to achieve this.
Method 1: Using the Top Command
The Linux “top” command displays a list of all the currently running processes and their PIDs. The command shows a live session on how the processes are running, and you can quickly find the target process and get its PID.
Let’s work with Firefox for this case. Open Firefox and then run the “top” command.
After running the “top” command, find Firefox from the list of processes displayed on your terminal. We can see that Firefox has a PID of 2448.
Method 2: Using the Pidof Command
If you know the name of the target process that you want to kill, you can use the “pidof” command. Its syntax is as follows:
Still working with Firefox, we can run the command, and we will get an output that shows all the PIDs associated with the target process. For our case, we have a few PIDs displayed as the output. Note how we still get 2448 which is the same PID that we got after running the “top” command.
Method 3: Using the Ps Aux Command
The last method to get the PID of a given process is using the “ps aux” command.
When you run the previous command, you will get an output of all the running processes and their PIDs. However, we can narrow this output by combining it with the “grep” command to filter only the PID of our target process.
Thus, if we are to check the PID of Firefox, our command runs as follows:
We get the different PIDs associated with Firefox. For instance, the first line in the output shows that Firefox has a PID of 2448 which is similar to what we got with the other commands.
How to Kill a Process with a PID in Linux
After getting the target process’s PID, the last step is to kill it. Note that once you execute the “kill” command, any instance of the target process will immediately shut down.
There are two ways in which you can kill a process using its PID:
Option 1: Using the -SIGTERM Signal
The “-SIGTERM” signal gets the job done, but it may get blocked sometimes which causes the process not to shut down.
To send a “-SIGTERM” kill signal, run the following command:
Using the PID that we got earlier, let’s run our “kill” command. Notice that we don’t get an output if we try to check the PID of Firefox. That confirms that the “kill” command has successfully worked.
Still, if you try to rerun the “kill” command using the same PID, you will get an error that “no such process” exists. That’s because the process has already been terminated.
Option 2: Using the -SIGKILL Signal
It is a more powerful option. When executed, it forcefully causes the target process to terminate and can’t be blocked. For instance, we open Firefox again and get its new PID. To kill it, we run the “kill” command and add -9 to represent the “-SIGKILL” signal.
Our command runs as follows:
Alternatively, you can run the same command as shown in the following:
You still get the same output.
Conclusion
The “kill” command lets you kill a process with a PID in Linux. You first get the PID of the target process. Afterward, you use the “kill” command to send a “-SIGTERM” or “-SIGKILL” signal to terminate the target process using its PID. Both options are demonstrated in this post.