- Home
- Logging Guide
- Linux kill process
How to kill a process in Linux
In this page
- What is a process in Linux?
- How to kill a process in Linux
- -Using the kill command
- -Using the killall command
- -Using the xkill command
- -Using the pkill command
- -Using the top command
- How EventLog Analyzer helps with Linux monitoring
If you’ve ever encountered an application that suddenly freezes or stops responding on your Linux system, you know the frustration. The application might still be running in the background, consuming system resources and preventing you from relaunching it. Unresponsive processes, whether consuming excessive CPU or memory, can seriously affect system stability. In this article, we'll learn how to identify and kill unresponsive processes using basic Linux commands.
What is a process in Linux?
Before diving into how to kill a process in Linux, it’s important to understand what a process is. In Linux, a process is an instance of a running program, each with a unique identifier called a Process ID (PID). Finding the PID of a process is essential, because it is needed by Linux commands to locate and kill the unresponsive process.
To find a process's PID, use these commands:
- "ps" command with the "-p" option: ps -p <PID> displays information about the process with the specified PID.
- "pidof" command: pidof <process-name> displays the PID of the process with the specified name.
How to kill a process in Linux
Linux offers these commands to kill a process using the command line:
- kill: Terminate a process by its PID in Linux.
- killall: Terminate processes by name in Linux.
- xkill: Terminate a process by clicking on its window (useful for graphical applications).
- pkill: Terminate processes based on name, user, or other attributes.
- top: Interactively monitor and kill processes directly from the command-line interface
To stop a process using its PID, it's essential that you know a process's PID.
Stop a process with the Linux kill command
The "kill" command lets you terminate processes by specifying their PID.
Syntax: kill <PID>
For instance, to terminate the Chrome process with PID 3827, you can use,
$ kill 3827
By default, this command sends the SIGTERM signal, enabling the process to shut down gracefully and complete any necessary cleanup before it terminates.The process with the specified PID will receive this signal and can save any data or close open files before terminating. This is the preferred method for terminating processes.
The "kill" command also allows you to specify other signals as well. The "Kill - l" command in the Linux terminal helps you view the complete list of all signals. Some of the other commonly available signals are listed below.
Signal | Description | Syntax |
---|---|---|
SIGTERM | Default signal sent by the kill command to terminate a process gracefully | kill -1 <PID> |
SIGKILL | Kill signal that forcefully terminates a process immediately without allowing a cleanup. | kill -9 <PID> |
SIGSTOP | Stop signal is used to pause or suspend a process. | kill -19 <PID> |
SIGCHLD | Signal sent to the parent process when a child process terminates or stops. | kill -17 <PID> |
Apart from these core signals to kill a process, Linux also offers other signals, as listed below, that are often used in combination with terminating a process.
Signal | Description | Syntax |
---|---|---|
SIGHUP | Hangup signal used to reload a process | kill -1 <PID> |
SIGILL | Signal that gets sent when a process tries to execute an illegal instruction | kill -4 <PID> |
SIGCONT | Continue signal that is used to resume a suspended process | kill -18 <PID> |
SIGNTSOP | Terminal stop signal sent when Ctrl+Z is pressed | kill -20 <PID> |
Terminate a process in Linux with the "killall" command
If you know the exact name of the process, the "killall" command can be used to terminate all processes with a specific name. For example, to kill all instances of Chrome, you would use: killall chrome
While the "killall" command is convenient, it may not always catch every instance of a process, especially if multiple versions or instances are running. In such cases, you might need to revert to using the "kill" command with specific PIDs.
Kill a process in Linux using the "xkill" command
The "xkill" command in Linux offers a quick and easy method to terminate unresponsive graphical applications. If you encounter a frozen window that won’t close, the "xkill" command allows you to terminate it by just clicking on the window.
To terminate a process with the "xkill" command, follow these steps:
- Launch the terminal: Find Open Terminal by searching for it in the applications menu or using keyboard shortcut (Ctrl+Alt+T) to launch the terminal.
-
Run the xkill command: After typing the command and pressing Enter, your cursor will change to a cross or skull icon.
Syntax: xkill
-
Click on the unresponsive window: With the cursor changed, simply click on the window you want to close. The process associated with that window will be immediately terminated.
The xkill command is especially useful when you need to quickly close an unresponsive application without searching for its process ID.
Kill a process in Linux using the "pkill" command
The "pkill" command offers a more flexible approach to killing processes in Linux. Unlike the "kill" command, which requires you to know the process ID, the "pkill" command lets you terminate processes based on their name, user, or other attributes.
To kill a process using the "pkill" command, follow these steps:
-
Terminate a process by name: This command kills all processes matching the specified name.
Syntax: pkill process_name
-
Target processes by user: To kill processes run by a particular user, use:
Syntax: pkill -u username process_name
Replace username with the user's name and process_name with the process you wish to terminate.
-
Use specific signals: By default, the "pkill" command sends the SIGTERM signal, but you can send a different signal if necessary. To forcefully terminate a process using SIGKILL, run:
Syntax: pkill -9 process_name
The "pkill" command is particularly useful when you need to terminate multiple instances of a process or manage processes in a multi-user environment.
Kill a process in Linux using the "top" command
The top command in Linux displays real-time information about your system’s running processes and resource usage. It provides an updated list of active processes, including details like CPU usage, memory usage, process ID (PID), user, and more.
While the "top" command is running, you can spot the process that you want to kill and follow the below steps:
-
Open the terminal, type top, and press Enter.This will display the real-time information of your system's processes and resource usage.
Syntax: top
- Locate the process you want to terminate by its PID. Press k while top is running.
- You will be asked to enter the PID of the process you wish to terminate.
- By default, top will use SIGTERM signal (15). You can press Enter to use this default signal or specify a different signal if needed.
How EventLog Analyzer helps with Linux monitoring
ManageEngine EventLog Analyzer, a comprehensive log management and compliance tool effectively monitors your Linux environment. It acts as a centralized syslog server that automatically parses syslogs from all Linux distributions to monitor its performance, health, and ensure security. This powerful log management tool provides insights into user logons, logoffs, critical security events, user account management changes, and more. Further, its file integrity monitoring (FIM) capabilities monitors Linux file system changes and alerts you in real-time about unauthorized modifications.