How to Kill a Process in Linux
In the Linux operating system, the ability to kill or terminate processes is a crucial and powerful feature that serves various purposes in managing and maintaining a system's performance, stability, and security. Killing a process is essentially a way to stop a running program or task.
Linux users, system administrators, and developers can all benefit from knowing how and when to stop processes. It enables efficient system performance management, problem solving, and the maintenance of system stability and security. Given that the improper strategy could have unforeseen repercussions, it is crucial to use caution when killing processes and select the right signal (such as SIGTERM or SIGKILL) depending on the situation.
Hostman offers a reliable managed Linux VPS for your projects.
How to Locate and Kill a Process in Linux Copy link
To locate a process that you want to terminate in Linux, you can use various commands and techniques. Here's how to do it:
You can terminate a process in Linux using a variety of commands and methods, depending on your needs and the situation. Here are a few popular methods for terminating a process:
-
Use the
psCommand
The ps command can list running processes. You can filter and search for a specific process using grep. Here’s the basic syntax:
root@stressfree-websites:~# ps aux | grep postfix
root 1538975 0.0 0.4 41964 4720 ? Ss 09:00 0:00 /usr/lib/postfix/sbin/master -w
postfix 1538977 0.0 0.7 42312 7052 ? S 09:00 0:00 pickup -l -t unix -u -c
postfix 1538978 0.0 0.7 42356 7096 ? S 09:00 0:00 qmgr -l -t unix -u
root 1539517 0.0 0.2 6852 2132 pts/1 S+ 10:30 0:00 grep --color=auto postfix-
Using
pgrepCommand
pgrep is a dedicated command for finding the PID of a process based on its name. The basic syntax is:
pgrep <process_name>-
Using
htoportop(Interactive Process Viewer)
The htop or top command provides an interactive process viewer. Run either of these commands, and you can search for processes, sort them, and even send signals to them directly from the interface. Here's how to use htop:
root@stressfree-websites:~# htopTo search for a specific process in htop, press the "F3" key and type the process name. Then, select the process and press the "F9" key to send a signal (e.g., SIGTERM) to it.
-
Using System Monitor (Graphical Interface)
Numerous Linux distributions provide a graphical task manager or system monitor. It makes it simple to observe and manage active processes. Usually, you can find it by looking for "System Monitor" or "Task Manager" in the system menu.
-
Using
pidofCommand (for Processes with Known Names)
You can use the pidof Command if you know the name of the process you want to terminate and it's a command that's directly executable
Once you've located the Process ID (PID) of the process you want to terminate, you can use the kill command with the appropriate signal to terminate the process.
Different Commands for Killing a Process Copy link
-
Using the
killCommand
The kill command is a general way to send signals to processes. The most commonly used signal is SIGTERM (15), which politely asks the process to terminate.
Below is unix machine with hostname “stressfree-websites”.
For example, to kill a process postfix with a process ID (PID) of 44259:
- Check and verify if postfix is running via below command:
ps -ef | grep postfixPID is showing 44259:
If the process is integrated with system, you can check the status of it using systemctl status <servicename>.
systemctl status postfix.service
- Given all details gathered on above, it is now ready to kill the process.
kill <PID>- Verify if postfix is still running:
ps -ef | grep postfix | grep -v grep | wc -lPostfix process with PID 44259 successfully killed. 0 meaning no process is running.
-
Using the
killallCommand
The killall command allows you to kill processes by name rather than PID. Be careful when using this command, as it can terminate multiple processes with the same name.
killall <process_name>For example, to kill all processes named "postfix":
- You can get the process name on the column 9 of the output below:
ps -ef | grep postfixThe process name is /usr/lib/postfix/sbin/master.
- Once process name identified, you can kill the process now.
killall /usr/lib/postfix/sbin/master- Verify if postfix is still running:
ps -ef | grep postfix | grep -v grep | wc -lPostfix process successfully killed. 0 meaning no process is running.
-
Using
pkillCommand
pkill is another command to send signals to processes based on their names or other attributes.
For example, to kill all processes whose name contains "postfix":
- Use this command:
pkill -f postfix- Verify if postfix is still running:
ps -ef | grep postfix | grep -v grep | wc -lPostfix process successfully killed. 0 meaning no process is running.
-
Using
killwith Signal Options
You can send different signals to a process depending on the situation. The default is SIGTERM, but you can use other signals like SIGKILL (9), which forcefully terminates the process.
- Check and verify if postfix is running via below command.
ps -ef | grep postfixPID is showing 1491887.
- To forcefully kill a process with PID 1491887:
kill -9 <PID>- Verify if postfix is still running:
ps -ef | grep postfix | grep -v grep | wc -lPostfix process with PID 44259 successfully killed. 0 meaning no process is running.
Remember that killing a process with SIGKILL (signal 9) should only be used as a last resort, as it does not allow the process to clear up resources and may result in data corruption in some instances. It is preferable to use SIGTERM first and SIGKILL only if necessary.
By the way, with Hostman, you can run your workloads on efficient VPS USA or Netherlands VPS that support low latency for EU-based users. For learning, demos, or small services, an inexpensive VPS server is often more than sufficient.