How to kill a process in Linux from the command line
In Linux, managing running processes can help maintain system health and performance. Sometimes, you might need to terminate a process to free up system resources or address unresponsive applications.
This practice, known as killing processes, ensures your system runs smoothly without unnecessary interruptions or resource drains. However, improperly stopping processes can lead to data loss or system instability.
In this article, you’ll learn various commands to safely end processes in Linux, such as kill, pkill, and killall. By the end, you’ll know how to manage and kill processes in a controlled manner effectively, ensuring optimal performance for your Linux machine.
Prerequisites
Before learning how to kill a process in Linux, make sure to follow these prerequisites:
- Linux-based machine. You need a computer or a server running a Linux distribution. If you don’t have one, consider purchasing a Linux-based VPS from Hostinger.
- Terminal or SSH access. Open a terminal on your local machine or use an SSH application like PuTTY to connect to your Linux server remotely.
- Root access or appropriate permissions. Most commands to kill processes require root or superuser privileges. Ensure you have the necessary permissions to execute them.
- Installed tools. Make sure to install the required tools and commands on your system. To verify, run either of the following:
sudo apt-get install procps x11-utils # for Debian-based distributions
sudo yum install procps-ng xorg-x11-apps # for Red Hat-based distributions
How to locate a process in Linux
You must first locate the correct process to avoid accidentally stopping essential system operations. Here are several commands to find details of running processes in Linux:
Locate a process with ps
The ps command provides a complete listing of running processes and their details, such as the user, process ID (PID), CPU and memory usage, and the command that started the process.
Here’s its basic syntax:
ps [options]
You can append these options to this command:
- -a – display information about all users.
- -u – include user-oriented output.
- -x – show processes without a controlling terminal.
For example, if you execute the following:
ps -aux
You should see an output similar to:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 169364 8824 ? Ss Jul08 0:05 /sbin/init
root 2 0.0 0.0 0 0 ? S Jul08 0:00 [kthreadd]
...
Find PID with pidof
The pidof command finds the process’s PID by name. It’s simple and handy when you already know the exact process name.
pidof [options] [program]
Some helpful options for pidof include:
- -c – return only the same root directory processes.
- -o – omit specified PIDs.
- -s – single shot (this will return the first PID).
- -x – scripts also (this will return processes that scripts have started).
For instance, to get the PID of sshd, run:
pidof sshd
If there is only one instance running, the output will be a single PID:
1234
However, if more than one instance is running, it will display multiple PIDs:
1234 5678
Find PID with pgrep
The pgrep command offers a more advanced way to find a process. It returns processes based on specific selection criteria that can match process names, known as a pattern.
pgrep [options] [pattern]
Here are some common options you can add:
- -f – match against the full command line.
- -l – list processes names and their IDs.
- -n – select the newest of the matching processes.
- -o – select the oldest of the matching processes.
- -u – only match processes whose effective user ID is listed.
- -x – match only processes whose name or command line exactly matches the pattern if -f is specified.
For example, to display the names and PIDs of all processes owned by the user john, you can use:
pgrep -lu john
The above command will return:
1234 process_name_1
5678 process_name_2
...
View running processes with top
The top command provides an interactive, real-time view of running processes. It shows the PIDs, users, amount of memory and CPU power each process uses, and running time.
To view a list of all active processes, run:
top
Here’s an output you would see:
top - 15:37:58 up 2:13, 2 users, load average: 0.00, 0.01, 0.05
Tasks: 95 total, 1 running, 94 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni, 100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 4045860 total, 3810124 free, 140408 used, 94808 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 3643028 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 169364 8824 6508 S 0.0 0.2 0:05.14 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
...
To exit the top interface, press Q.
Understanding kill command signals
kill command signals are messages sent to processes to instruct them to perform specific actions. They are a fundamental part of Unix-based operating systems, allowing users and programs to control processes effectively.
You can view all available signals on Linux with the command below:
kill -l
It lists all signals along with their corresponding numbers and names, such as:
- SIGTERM (Signal 15). This signal asks for a process to terminate. The process can capture this signal, perform cleanup operations, and then exit. By default, the kill command sends SIGTERM if no other signal is specified.
- SIGKILL (Signal 9). It forcefully kills a process. The process cannot capture or ignore this signal, which results in an immediate termination. It should be used as a last resort when a process doesn’t respond to SIGTERM.
- SIGINT (Signal 2). This is typically sent when you press Ctrl + C in the terminal. It interrupts a process and is usually used to stop a process running in the foreground.
- SIGHUP (Signal 1). Sent to a process when its controlling terminal is closed and often used to reload configuration files.
- SIGQUIT (Signal 3). Causes a process to terminate and produce a core dump file for debugging.
- SIGSTOP (Signal 19). Pauses a process without killing it, similar to pressing Ctrl + Z in the terminal.
- SIGCONT (Signal 18). Continues a process that was stopped by SIGSTOP or Ctrl + Z.
For instance, to kill a process with SIGTERM, execute the following:
kill -15 [PID]
The importance of choosing the appropriate signal
Using the correct kill signal based on the scenario and the desired impact ensures that processes are terminated in a controlled and predictable manner, especially when dealing with background tasks.
For example, the SIGTERM signal is preferred for safe process control, letting processes carefully save their state and exit. SIGKILL, while practical, should be sparingly used as it may lead to data loss or corruption.
Suggested Reading
Follow our article to learn about some of the most commonly used Linux commands, strengthening your skills and familiarity with this operating system.
How to kill processes in Linux
In this section, we’ll explore various ways to end processes in Linux for different scenarios.
How to kill a single process with PID
If you know the PID of the process you want to terminate, you can easily use the Linux kill command to send a signal. The following is its basic syntax:
kill [signal] PID
For example, to ask the process to end with the default SIGTERM signal and PID 1234, execute:
kill -15 1234
If the process does not respond to SIGTERM, try using the SIGKILL signal to terminate it forcefully:
kill -9 1234
You can also send other signals based on your specific needs, such as:
kill -2 1234 # Sends SIGINT
kill -1 1234 # Sends SIGHUP
How to kill multiple processes
There might be situations where you need to terminate multiple processes simultaneously. Fortunately, the kill command can also send a signal to multiple PIDs at once, making it a powerful tool for managing Linux processes.
However, be cautious, as some processes might depend on others. Killing a parent process can also terminate other processes.
The basic syntax for killing multiple processes is:
kill [signal] PID1 PID2 PID3
Here’s an example of ending processes with PIDs 1234, 5678, and 91011 using SIGTERM:
kill -15 1234 5678 91011
To send a signal to all processes in a group, use the negative PID of the group leader:
kill -15 -1234
This kills all processes in the group led by PID 1234.
How to kill a process with the pkill command
The pkill command lets you kill processes by name without first identifying their PIDs, making it simpler and more convenient to use than kill. It’s also faster to type and execute, especially when dealing with multiple instances of the same process.
Below is the basic syntax of pkill:
pkill [signal] [process name]
For example, to kill a process named nginx, run:
pkill -SIGTERM nginx
If you want to terminate processes owned by a specific user:
pkill -u john
For instance, to end processes associated with a terminal pts/1:
pkill -t pts/1
You can also kill all processes in a specific process group, for example, PGID 1234:
pkill -g 1234
To terminate the newest or oldest instance of a process, type the following:
pkill -n nginx # Kill the newest instance of nginx
pkill -o nginx # Kill the oldest instance of nginx
You can also combine multiple options to match processes based on specific criteria. For example, to kill processes from user john on terminal pts/1 in process group 1234 at once, execute the following:
pkill -u john -t pts/1 -g 1234
How to kill a process with the killall command
killall terminates all instances of a process by name and can target processes based on age, user, and running time.
It’s ideal to manage processes with the same name across the system, compared to kill for individual processes and pkill for selectively managing multiple instances of a process.
The basic syntax of the killall command is:
killall [options] [process_name]
Here are some of its primary uses:
killall nginx # Terminates all instances of nginx
killall -s SIGKILL nginx # Forcefully kills all instances of nginx
killall -u john nginx # Ends all nginx processes owned by the user john
If you want to kill processes that have been running for a specific duration:
killall -o 1h nginx
This kills all nginx processes running for more than one hour.
To kill all instances of a process except a specific one:
killall -v -I nginx
You can exclude a specific nginx process from being killed with the above command.
Furthermore, to kill processes based on their CPU load or memory usage:
killall -i -s SIGKILL nginx
This sends an interactive prompt before killing each nginx process so you can confirm or deny each action.
How to kill a process with the xkill command
The xkill command is designed for GUI-based environments like a Linux-installed laptop. With it, you can kill applications that have frozen or don’t respond to standard termination commands with a simple click.
Here are some scenarios where xkill proves handy:
- Unresponsive applications. If an application becomes unresponsive and you cannot close it using standard methods.
- Frozen GUI elements. When certain GUI elements, such as windows or dialogs, freeze and don’t allow interaction.
- Quick termination. When you need to terminate a specific window or application quickly and easily without looking up its PID.
To use xkill, enter this command in your terminal:
xkill
After executing this command, your mouse cursor will change to an × icon. Then, move the cursor to the window of the unresponsive application and right-click on it. This will immediately kill the selected process.
Alternatively, if you know the unresponsive application’s window ID, type:
xkill -id <window_id>
With this command, you can target and kill a specific window directly.
Please note that xkill doesn’t natively support killing by class or name. However, you can use other tools in conjunction with it to achieve similar results. For example, use wmctrl to list windows and their IDs, and then run xkill with the appropriate ID.
To install wmctrl, run:
sudo apt-get install wmctrl
Once done, list all windows and their IDs:
wmctrl -l
The output will look something like this:
0x03c00003 0 hostname Firefox
0x04a00004 0 hostname Terminal
0x05000005 0 hostname Code
Then run xkill with the desired ID, for instance:
xkill -id 0x03c00003
How to kill a process with the top command
Last but not least, the top command provides a real-time, interactive view of resource usage and running processes. It’s handy for identifying and terminating processes that consume excessive resources or become unresponsive.
As previously explained, you can type top in your terminal to start it. To navigate through the list of processes, use the arrow keys. Press Shift + P to sort processes by CPU usage, while Shift + M sorts them by memory usage.
...
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 169364 8824 6508 S 0.0 0.2 0:05.14 systemd
512 user 20 0 120364 6024 2508 S 1.0 0.1 0:02.04 firefox
789 user 20 0 140364 7624 3208 R 2.0 0.3 0:03.22 chrome
...
Once you’ve identified the process you want to end, press K.
top will prompt you to enter the process’ PID you wish to kill. Then, you need to specify the signal to send to the process.
Press Enter to send the default SIGTERM signal, or type a different signal number if required.
For instance, to kill the chrome process with PID 789 using SIGKILL:
k
PID to kill: 789
Send signal (15): 9
Additionally, to view processes owned by a specific user, start top with the -u option like the following:
top -u john
You can also customize the columns displayed within top by pressing F. This way, you can add or remove columns and sort by different criteria.
Conclusion
In this article, we covered various methods for terminating processes in Linux, including the kill, pkill, killall, xkill, and top commands. Each command offers different functionalities, allowing you to use them according to your needs.
Carefully managing processes is essential for maintaining system performance and stability. By familiarizing yourself with these commands, you can effectively handle unresponsive applications and optimize resource usage.
Lastly, always verify the processes you terminate to avoid accidental system issues and ensure your Linux machine runs smoothly.
How to kill a process in Linux FAQ
This section answers some of the most common questions about killing a process in Linux.
What processes can you kill in Linux?
You can kill both user and system processes in Linux. User processes owned by the user can generally be killed without issues. System processes owned by the system or root user require root permissions to kill and should be handled cautiously, as terminating them can cause system failure.
What is the difference between the kill and killall commands?
The kill command requires a PID and terminates a specific process, making it ideal for precise control. Meanwhile, the killall command terminates all instances of a process by name, offering more flexibility for managing processes with the same name across the system.
How do I find the PID of a process I want to kill?
You can find the PID of a process using the ps, pidof, pgrep, or top commands. For example, use ps aux, pidof [process_name], pgrep [process_name], or top to list process IDs and details.
Comments
December 21 2020
You can also use lsof to get a list of ports as is shown here! https://fjolt.com/article/linux-how-to-kill-process-at-port