Kill Process in Linux Using kill Command
Kill process in Linux using kill command involves identifying the process ID (PID) and sending termination signals like SIGTERM or SIGKILL. This method safely or forcefully stops unresponsive or resource-heavy processes to maintain system stability.
🤖AI Overview:
The kill command in Linux enables users to terminate processes by sending signals such as SIGTERM or SIGKILL. It requires the process ID and appropriate permissions, enabling efficient command-line management of active or unresponsive processes. This command is essential for maintaining optimal system resource management and stability.
How to kill process in Linux Using kill Command?
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:
top
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.
Thetop
command 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 theps
command is as follows:
ps <options>
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:
ps -aux
The options that can be executed with theps
command includes the following:
Options | Description |
---|---|
-a | It provides access to all running processes instead of being limited to the processes belonging to the current user. |
-u | Provides more details about each process. |
-x | 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.
Using thepgrep
command 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 thepgrep
command 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 thepgrep
command, you can use other options to obtain specific information from Linux processes, which include the ones we present in the table:
Options | Description |
---|---|
-n | Displays the newest process. |
-l | Provides a list of PIDs and process names. |
-o | Displays the oldest process. |
-x | It provides only processes that meet the criteria defined in the pattern. |
-u | You can search for processes belonging to specific users. |
-
Obtaining the process ID number using the pidof command
You can also use thepidof
command 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 thepidof
command is as follows:
pidof <options> <program>
You can execute different options for different purposes with thepidof
command; we presented these options in a table:
Options | Description |
---|---|
-c | The provider of PIDs is in the Root directory. |
-s | It displays only one PID. |
-o | You can kill a process by typing its PID after flag-o. |
-x | 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:
kill -l
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:
kill <processID>
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:
-signal
-
Using the command kill -9 to kill processes
If thekill <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>
Or
kill -SIGKILL <processID>
Executing the commandkill -SIGKILL <processID>
or kill -9 <processID>
, will definitely destroy the incorrect process that the commandkill <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 thekillall
command is useful. Several processes can be killed by executing thekillall
command, whose main syntax is as follows:
killall <process>
Along with theKillall
command, you can also use other options:
Options | Description |
---|---|
-e | It looks for an exact match with the process name. |
-I | It is used to eliminate attention to capitalization and lowercase letters of the process name. |
-i | Used to add additional confirmation to kill the process. |
-v | Provides a report to ensure the successful termination of the process. |
-u | Terminates processes belonging to a specific user. |
-o | Flag-o along with a specific time period, kills the processes that are executed more than this specific period of time. |
-y | Flag-y along with the specified time kills processes that have been executed for less than the specified time. |
One of the advantages of thekillall
command 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 thexkill
command is as follows:
xkill <resource>
Running thexkill
command 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. Thepkill
command, like thekillall
command, uses the SIGTERM signal to kill processes. The difference betweenpkill
andkillall
is thatpkill
matches a specific pattern, whilekillall
kills the process based on its name.
The main syntax of thepkill
command is as follows:
pkill <options> <pattern>
For example, to kill the Firefox browser process, you can run the following command:
pkill Firefox
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 thepkill
command:
Options | Description |
---|---|
-u | It kills the processes that were executed by the specified user. |
-n | It kills the newest processes. |
-o | It terminates the oldest processes. |
-signal | It changes the default signal of the pkill command (SIGTERM). |
-x | 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.
Conclusion
Effectively learning how to kill process in Linux using kill command is critical for developers managing Linux systems.
This tool provides flexible and direct control over processes through signal handling. Understanding process IDs, selecting appropriate signals, and practicing best methods ensures minimal disruption and maintains system stability.
By adhering to these principles, developers can maintain efficient process control, troubleshoot unresponsive applications, and optimize system performance in diverse Linux environments.
FAQ
2. How do I find the Process ID (PID) of a process?
Use commands like "ps, top", or "pgrep" to list processes and their PIDs.
3. What is the difference between SIGTERM and SIGKILL?
"SIGTERM" politely requests process termination allowing cleanup, while "SIGKILL" forcefully stops the process immediately.
4. Can I kill multiple processes at once?
Yes, by listing multiple PIDs in one kill command, for example, "kill -9 pid1 pid2 pid3".
5. How can I kill a process by name?
Use "killall" for exact name matching or "pkill" for pattern matching of process names.
6. Do I need special permissions to kill processes?
Root or sudo privileges are needed to kill processes owned by other users; normal users can kill their own processes.
7. What is the xkill command used for?
"xkill" forcibly closes GUI applications by selecting the window to terminate.
8. How to verify a process has been killed?
Check the process list again using "ps" or "top" commands.
9. Which packages provide kill and related commands?
"Kill, pkill, ps", and "to"p are part of "procps"; "killall" belongs to "psmisc".
10. Are there GUI alternatives to kill process?
Yes, most Linux desktops have system monitors or task managers for process termination.