How to Kill Process in Linux using kill Command Types
Until now, you must have been in a situation where you have launched a program in the Linux operating system and while you are using the program, it suddenly crashes and remains unresponsive. In this situation, most users stop and restart the program, but still, the program froze up. In such a situation, users do not know what to do to run the program correctly and do not know the cause of the failure. When you stop and restart the application, the application will not run correctly because the main request process is not completely stopped.
In the Linux operating system, running tasks are known as Processes. To solve this problem, the system must either be restarted or if your program still froze up after restarting the system, the Process must be killed or stopped. but how?
As in Windows, the task manager can manage problems similar to the problem of unresponsive processes and programs, fortunately, in Linux, there are various methods and services to kill and stop unresponsive processes.
It is also possible to kill processes in Linux through the command line (pressing CTRL+C keys) and the graphical user interface, but it will be easier to do this task using the command line. Linux is an advanced and excellent operating system that provides a solution for every problem; don’t worry about anything when using Linux VPS because your problems will be easily solved, you just need to improve your knowledge and awareness about using Linux and be familiar with useful Linux commands. By learning how to kill processes in Linux, you can manage your Linux VPS better and more efficiently.
In this article, we will teach how to kill incorrect processes in Linux using various Kill commands, which is one of the most common Linux commands. You will also learn how to terminate a process that is not working properly or is consuming too much system resources.
How to kill processes in Linux?
You can terminate the wrong processes by identifying the running processes and accessing information about each process, as well as by entering a variety of Kill commands. One thing you should pay attention to is that to kill all processes, you must have root user permission or sudo privileges, otherwise, as a normal Linux user, you can only kill your own processes and you won’t be able to kill processes that belong to other Linux users.
Different signals are sent to terminate a specific process, the most common of which are SIGKILL and SIGTERM. The SIGTERM signal tries to kill the process and sometimes fails to terminate it, but the SIGKILL signal is more serious and risky because it quickly kills any type of process and leaves no room for retrying to terminate the specific process or the possibility of its blocking.
What are the uses of the kill command?
- It Shuts down the server or the program that you have already started and forgot.
- ItTerminates the incorrect running process.
- It Eliminates the process that consumes too much system resources.
- It Kills the process that is running automatically.
- Terminating a process that was accidentally executed.
- It Eliminates the background process.
- Sending a signal to the process to terminate the process.
Viewing information about running processes in Linux
Viewing information about running processes using the top command
To execute the kill command to stop the process, you must first have access to a list of running processes in Linux and their information. To see a list of running processes, run the following command:
The output of the above command displays a list of processes along with process IDs and users and information about the amount of memory used and CPU power.
topcommand interface allows you to terminate a process by pressing the K button and typing the desired process ID, without executing another command from the provided list.
Note: Press the q button to exit the top command interface.
Viewing information about running processes using the ps command
The ps command finds all the running processes in Linux and presents them in a list format. The difference between the output of the ps command and the top is that the ps command does not act as an interface, but it displays complete information from PIDs, CPU power, and other details from running processes. The main syntax of the
pscommand is as follows:
Also, the ps command provides Linux users with customized details of the running processes, along with the tags that we will mention below. One of the common options to display more information about running processes is “-aux“, which is executed in the form of the following command:
The options that can be executed with the
pscommand includes the following:
|It provides access to all running processes instead of being limited to the processes belonging to the current user.
|Provides more details about each process.
|Provides processes that are managed by daemons.
To access specific information about running processes in Linux, such as displaying java running processes, you can also use the following command combination:
ps -ux | grep java
Finding PID and process details using the pgrep command
In addition to the previous method, by executing the top and ps commands, users could access a list of running processes. There are other ways to search for specific processes along with their information. Linux users can find the process ID number (PID) or process name by running various commands and then killing them.
pgrepcommand is not as simple as the previous commands, but it is a way that users can customize specific patterns and criteria to find a specific process. The main syntax of the
pgrepcommand is as follows:
pgrep <options> <pattern>
Note: In the pattern that is defined, the symbol * is general, and a* is one of the widely used options that is used as the pattern expression.
For example, if you want to access the processes managed by the Root user, run the following command:
pgrep -u root
If you want to find processes that start with the letter “a” and belong to the Root user, the following command will get you what you want:
pgrep -u root 'a*'
Along with the
pgrepcommand, you can use other options to obtain specific information from Linux processes, which include the ones we present in the table:
|Displays the newest process.
|Provides a list of PIDs and process names.
|Displays the oldest process.
|It provides only processes that meet the criteria defined in the pattern.
|You can search for processes belonging to specific users.
Obtaining the process ID number using the pidof command
You can also use the
pidofcommand to view the information and PIDs of the processes, with the difference that you must know the names of the processes to access the PIDs of the processes. The main syntax of the
pidofcommand is as follows:
pidof <options> <program>
You can execute different options for different purposes with the
pidofcommand; we presented these options in a table:
|The provider of PIDs is in the Root directory.
|It displays only one PID.
|You can kill a process by typing its PID after flag-o.
|This option displays the PID of the shells that execute the scripts.
How to use all types of Kill commands to terminate processes
At the beginning of the article, we explained that the Kill command sends a signal to terminate the processes. To terminate processes, there are various signals in Linux, to see the signals of the Kill command, you must run the following command:
In the output of the kill -l command, various signals are displayed along with their names and numbers, but as we mentioned at the beginning of the article, usually two signals SIGKILL (9) and SIGTERM (15) are sent.
The Kill command has various uses in terminating processes. In the following, we will introduce you to the types of Kill commands and their applications in killing processes.
Using the Kill command to terminate processes with PID
After accessing the process ID, you can kill a specific process by executing the Kill command:
It should be noted that the Kill command automatically uses the SIGTERM signal to terminate processes, but you can use the following command to change the default signal:
Using the command kill -9 to kill processes
kill <processID>command fails to kill the desired process, you can use the following command as an alternative, having the PID of the specific process:
kill [Signal_or_Option] pid
In the previous command, you can use the desired option or signal to definitely kill the processes and in this way determine a specific signal.
The shorter form of the previous command can also be executed as follows:
kill -9 <processID>
kill -SIGKILL <processID>
Executing the command
kill -SIGKILL <processID>or
kill -9 <processID>, will definitely destroy the incorrect process that the command
kill <processID>could not terminate. Executing the previous commands bypasses the standards of shutting down processes and destroys any kind of process and data. The SIGKILL service kills all processes, if this does not happen, you should suspect a malfunction in your operating system.
Running the Kill command to kill multiple processes
Users can kill several processes at the same time using the kill command:
kill -9 pid1 pid2 pid3
Running the killall command to kill processes by name
When killing unresponsive processes, if you don’t have the process ID numbers, but the process names are available, running the
killallcommand is useful. Several processes can be killed by executing the
killallcommand, whose main syntax is as follows:
Along with the
Killallcommand, you can also use other options:
|It looks for an exact match with the process name.
|It is used to eliminate attention to capitalization and lowercase letters of the process name.
|Used to add additional confirmation to kill the process.
|Provides a report to ensure the successful termination of the process.
|Terminates processes belonging to a specific user.
|Flag-o along with a specific time period, kills the processes that are executed more than this specific period of time.
|Flag-y along with the specified time kills processes that have been executed for less than the specified time.
One of the advantages of the
killallcommand is the ability to customize the timestamp for terminating processes. For example, you can specify that the process that is running for less than 20 minutes will be killed with the following command:
killall -y 20m [Process_Name>]
For example, if you want to kill processes that run for more than 20 minutes, you should run the following command:
killall -o 20m [Process_Name>]
As you know, the components s (seconds), m (minutes), h (hours), d (days), w (weeks), M (months), and y (years) can be placed in the previous command and terminate processes based on their age.
Note: It should be noted that the Killall command uses the SIGTERM signal by default to kill processes.
Using the xkill command to block the connection between the server and the client
This command is used to close the processes that are running unintentionally on the server, in fact, it prevents the connection from the server to the client. The syntax of the
xkillcommand is as follows:
xkillcommand without specifying the source allows the process to be closed by selecting them in a window.
Using the pkill command to kill processes by name
The pkill command is able to kill unresponsive processes using a specific pattern or process name. The
pkillcommand, like the
killallcommand, uses the SIGTERM signal to kill processes. The difference between
pkillmatches a specific pattern, while
killallkills the process based on its name.
The main syntax of the
pkillcommand is as follows:
pkill <options> <pattern>
For example, to kill the Firefox browser process, you can run the following command:
The previous command is useful when you know the full name of the process. If you remember the partial name of the process, use the following command instead of the previous command:
pgrep -l Fir
In the following, we will present the options that are used using the
|It kills the processes that were executed by the specified user.
|It kills the newest processes.
|It terminates the oldest processes.
|It changes the default signal of the pkill command (SIGTERM).
|Kills processes according to the pattern.
How to kill the running process in Linux via GUI?
For some users, it is easier to apply the settings through the GUI, that’s why they prefer to apply the Linux settings through the GUI. You can use the Linux GUI to kill the running process. Fortunately, most Linux distributions provide Gnome, KDE, or another graphical user interface by default, so that users can freely decide between the terminal environment and GUI to perform their tasks. Follow the process below to kill the running process through the GUI:
- Find the Task Manager or the system monitor in the system menu of your favorite Linux desktop environment.
- Click on the process tab. You will come across a page containing a list of running processes. Select the process you want to terminate. You can search for the process related to the program you want through the magnifier (search) icon.
- By selecting the running process, click on the End Process button to kill the process. You can also terminate the process by right-clicking on the running process and selecting the Kill/End option from the context menu.
Note: On the page, you are viewing, in the process tab, you can also access information such as the process ID of the running processes.
As a result, you succeeded in killing the running process through the GUI.
When a process in Linux is not executed correctly or is executed unintentionally, you can terminate the process by executing the Kill command. Usually, the Kill command is useful if you want to kill a process that runs automatically in the background or a process that uses too much system resources.
By executing the
kill-3command, you can get a list of Java threads in the Java virtual machine. The command kill -3 works as a thread dump.
By executing different types of kill commands, a signal is sent to the running process to stop the desired process. Before executing the kill command, you must access the information and ID of the processes and then terminate the wrong processes by executing the kill command.
If you want to create an interrupt, you can enter the
Kill -2command, in this case, the process that is not working properly will be shocked and then the loop will continue. The number 2 in the command Kill -2 is the same value set for SIGINT. You must send 15 (SIGTERM) to stop the process completely.
When the service becomes unresponsive in Linux, run the kill, killall, and pkill commands to terminate the program. You can also use the ctrl+c shortcut key to close the program that has frozen.
It is very easy to kill processes that are not running properly in Linux by executing various kill commands. When you cannot stop a process in other ways, for example through the GUI or rebooting the system, the most efficient way is the Linux terminal. In this article, you learned that to terminate a process in Linux, SIGKILL or SIGTERM signals, which are the most widely used, are sent.
The first step you must take to kill a process is to access the information and PID and names of the running processes, for this purpose
pgrepcommands can be used, and after knowing the name and ID of the processes, you can kill the desired process by executing the commands
The types of Kill commands are similar to each other and the difference is that the Kill command kills the processes using their PID, but the Killall and pkill commands terminate them using the names of the processes and other criteria. Also, their other difference is in the root package of the commands, The ps, top, kill, pkill commands belong to the procps package, while the killall command belongs to the psmisc package.
Each type of kill command is used for specific purposes and is the fastest and safest way to solve your problem of killing incorrect processes. We hope that reading this article has solved your problem.
Thank you for your choice.