In some situations, processes in Linux can prevent users from starting tasks or installing applications for instance when an apt command is invoked , an error similar to ’could not get lock /var/lib/dpkg/lock-frontend’ might occur. This might be due to the fact that the apt command is being used by another process and in such circumstances killing that process might solve the issue. Killing a process actually means to force it to quit. This makes it possible for users to sort out problems without actually rebooting.
In this tutorial you we will first briefly describe what processes are in Linux before delving into how to kill a process in linux properly.
When you run chrome on your Linux system, open up your terminal or create a cron job, you are actually creating processes. Now there are two types of processes :
These are processes that had started automatically without any interaction or intervention with the user.
These are processes that were launched or started directly by users.
Now when a process starts, for instance when you launch Chrome, it will be assigned a PID or unique process identification number. Each process in Linux has a unique 5-digit pid.
When Linux boots, the first process launched is :init process. This is why it has PID of 1 in most of the cases. ‘Init’ , which is the master process, can not be killed like other standard processes .
For instance to find the processes relate to apcahe2 server, run the command below :
ps aux | grep apache2
The first number on each line is the PID. To get only the PIDs, execute the command below :
Where you can see that the PIDs above match those on displayed by the previous command.
As an alternative, the command ‘ps -A’ shows the PID along with the process name as well :
Sometimes when a process gets stuck or prevents other tasks from running or an application becomes unresponsive , Linux provides a method to kill such hanging or misbehaving processes. In order to terminate a specific process, the kill command should be used (there are also killall and pkill that will be shown further below). This will require the PID of the process you wish to kill.
It has the syntax below :
kill [signal or option] PID(s) [kill process Linux]
Regarding the permissions required to carry out the kill command, these are simple :
– You can kill the processes owned by you
– System level processes and processes launched by other users, can only be killed by root user.
The Kill command actually sends a rather specific signal to a process. If no signal is provided, the TERM signal is sent by default, which kills the process. To display the list of all Linux signals and their descriptions, run the command :
You can use also the command :
kill process ubuntu
which shows all available signal names.
The most frequently used signals are : INT, HUP, STOP, KILL, CONT and 0.
Signals may be provided in the following three ways:
- by number (e.g., -15)
- using the “SIG” prefix (e.g., -SIGKILL)
- without using the “SIG” prefix (e.g., -KILL).
For a kill command, the signal name could have the following values:
linux kill command signals
The safest option to kill or terminate a process is the SIGTERM which is the default as mentioned above.
Now let’s see how to kill process. We should first look up the PID by using for instance ps -A.
Once we have the PID of the process to terminate, issue the command
kill -9 PID
For instance let us kill the Firefox process. In the snapshot below, you can see that there is an active firefox browser (red dots next to the Firefox icon) :
We can also see the PID which is 18447. In order now to terminate it, we invoke the command kill -9 as follows :
kill -9 18447. [Linux kill process]
kill linux process
You can see above that the red dots besides the Firefox icon are gone. In order to thoroughly check that the process was terminated , we can choose to run any of the following commands :
ps -A | grep firefox
ps aux | grep firefox
For instance :
Which confirms the kill.
From the table above, a similar command can be used as follows :
kill -SIGKILL 18447
You can also kill many processes at a time as follows :
kill pid1 pid2 pid3
The pkill command is used to kill one or several processes by the process name. You do not need the PID anymore. In case you mistype the process name however, you might terminate another process by mistake. To be one the safe side, you can use the command pgrep as follows :
This will return one PID of the process you want to kill, .i.e. here it is Firefox. Now you can use pkill safely as follows :
the pkill command has indeed terminated the Firefox process as shown above(no red dots next to the Firefox browser icon).
Warning: On Solaris, the killall utility will terminate all processes that belong to the current user. Proceed with caution.
If a process has many instances along with multiple child processes, the command ‘killall‘ is the appropriate tool. killall operates similarly to the pkill command but instead of passing a search term as an argument you have to specify the full exact process name.It has the following syntax :
killall [signal or option] Exact_Process_Name
For instance, to kill all ‘Web Content’ instances (tabs of Firefox) and their child processes :
run the command as follows :
killall -9 ‘Web Content’
killall command in linux
As you can see from the snapshot above, you would need to specify the process name between quotes since spaces are not allowed.
This has crashed the Firefox tabs as shown above !
The -y (younger than) killall option is used to kill processes that have been up and running for less than the desired period. The period is specified in numbers followed by:
s (seconds) , m (minutes), h (hours), d (days), w (weeks), M (months, capital “M”) and y (years).
For example, to kill a process named FOX that has been started less than 5 minutes ago and keep other older instances of FOX running, you could proceed as follows:
killall -y 5m FOX. [how to kill running process in Linux]
The -o (older than) killall option is used however to kill processes older than a desired period of time. The following example will terminate all older-than-2-days httpd connections:
killall -o 2d httpd
For more information on the killall utility, visit the man page :
The aforementioned commands allow users to identify and kill unwanted, misbehaving and malfunctioning processes safely and appropriately.
Precaution must be taken however. Firstly, identify correctly the process to eliminate, i.e. make sure the process you’re about to terminate is the correct one. Secondly, enter the full name of the process and never a substring of it.
If you like the content, we would appreciate your support by buying us a coffe. Thank you so much for your visit and support.