A process is the instance of a computer program that is being executed by one or many threads. It contains the program code and its activity. Several processes may be associated with the same program; for example, opening up several instances of the same program often results in more than one process being executed.
kill is a command which is used in several popular operating systems to send signals to running processes. In Linux kill command is normally used to terminate running process. In this article we will cover basics of kill command so you will understand it and apply it in your environment.
Before killing or terminating any process we have to get its process ID (PID) or process name. We can use wo commands to find process ID’s, first is “ps” command and the second it “top” command. In this article we will use ps command to find a process ID and its name.
Locate process using ps command
ps command is used to see processes, following command to list all running processes.
ps aux
a: Show processes for all users
u: Display the user who is using the process
x: Show all processes. (it also show the processes running in a GUI environment)
List specific process ID
If you want to search any specific process so you can use any of following three methods.
Method 1:
ps -aux | grep process-name
e.g check for Apache process
ps -aux | grep httpd
You can see process id’s of Apache in above image.
Method 2: Use pidof command
pidof httpd
Sample outputs:
4372 4371 4370 4369 4368 4361
Method 3: Use pgrep command
pgrep httpd
All above methods will show process ID’s of httpd (Apache) process.
Kill Process
kill command is use to terminate or kill running processes.
Method 1: Kill process by its process ID
Now, I will kill one Apache process using its process ID.
kill -9 4372
Then, verify that PID is killed
pidof httpd
So you can see in above image that process 4372 is no more exist.
Method 2: Kill process by its name
If you want to kill all the processes of a service you can use this method.
kill -9 httpd
OR
pkill httpd
Now, verify process is killed.
pidof httpd
Then, you will see that all processes of httpd will no more exist.
Kill all processes of a User
You can use below command to show processes of a user.
ps -fu user-name
Now you will get process ID’s of that user. E.g I show process ID’s of user “osradar” in below image
Then, run below command to kill all processes of the user
kill -HUP process-ids
So, when you run again below command you will see no result.
ps -fu user-name
That’s it, You now know that how to kill running processes in Linux.