In Linux, a process is an instance of a program that is running on a computer. When a program starts executing it can have multiple processes associated with it. These processes can either run by a user or by the operating system itself. Usually, a process terminates on its own when they are done with their task.
Sometimes a process hanged up and starts consuming a lot of system resources such as CPU or RAM. Linux provides kill command to terminate such a process from the system. Ahead in this article, we will discuss finding and killing a process in Linux or Unix.
Process Identifier (PID)
The process identifier or PID is a number that is used by an operating system to identify an active process uniquely. PID can be used for tasks such as changing the priority of execution or killing processes altogether.
List the running processes in a system –
You can use top utility to see a list of running processes in a system with some other information regarding resource utilization and the priority of a process. Use the following command in your terminal –
top
This will display like given in the image below. Here you can identify a process with its process id if you want to kill it.
Or you can find the process id of a process by its name. To find PID use the following command in your terminal –
pidof gnome-shell
Or to find the process id of firefox use –
pidof firefox
You can see the output of the pidof command in the given images.
Linux processes and signals
A signal is a notification, a message sent by either the operating system or some application to a program. In Linux when a process is killed a terminating signal is delivered to the process. Linux kernel implements about 30 signals, each of them is identified by a number, from 1to 31. Some commonly used signals are given below –
- SIGHUP(1) – Hangup a process. Use it to reload configuration files or open and close log files.
- SIGKILL(9) – Kill signal. Use it to forcefully kill a process.
- SIGTERM(15) – Termination signal. It is the default and safest way to kill a process.
- SIGSTOP(17,19,23) – Stop process
How to kill a process in Linux?
There are various commands such as kill, pkill, killall, etc that you can use to kill a process in Linux. The kill command is very straightforward all you need to have PID to kill a process. Now run the following command –
kill PID
For example –
pidof vlc
Here process id of vlc is 5402. To kill this process use the following command in your terminal –
kill 5402
To kill vlc forcibly use –
kill -9 5402
How to use pkill command to terminate a process Linux?
The pkill command allows you to kill the processes based on partial matches. Suppose you want to kill the processes containing the name vlc in their name. Then use the following command in your terminal –
pkill vlc
To forcibly kill all the processes associated with vlc use-
pkill -9 vlc
How to use the killall command to kill the processes?
This is one of the easiest ways to kill a process from the terminal. Now if you know the exact name of the process then you can use the following command in your terminal –
killall vlc
Or to kill it forcibly use –
killall -9 vlc
Conclusion
In this article, you learned to kill unwanted processes. Now if you have any queries related to this topic then you can write to us in the comments below.