How to Kill all Processes for a User in Linux
There is nothing more frustrating than logging into your Linux system only to have many processes slowing down your machine. Although Linux is very good at process management, you may need to terminate some processes manually.
In this tutorial, we shall discuss how to find and kill processes from a specific user. We will use tools such as pkill and killall.
NOTE: Ensure you have access to an administrative account or the root account because regular users can only kill their processes, not processes belonging to other users. On the other hand, the root user can kill all processes.
Let us get started.
Understanding System Signals
To kill a specific process, the command sends a signal to the process. By default, kill commands such as killall send a TERM signal that gracefully stops the specified process.
Before we proceed to kill command, let us take a brief overview of the process signals.
The common types of kill signals include:
- SIGHUP – This signal reloads a process. It has an integer value of 1.
- SIGINT – Interrupt from the keyboard such as CTRL + C. It has an integer value of 2.
- SIGKILL – Kill a process. It has an integer value of 9.
- SIGTERM – This is a termination signal, which, as stated, gracefully stops a process. It has an integer value of 15.
- SIGSTOP – Stops a process. It has an integer value of either 17, 19, or 23.
NOTE: When terminating processes, it is good to start with the least aggressive signal, such as TERM. Using a signal such as TERM will allow the program to shut down correctly and use the set measures when the program receives the TERM signal.
A KILL signal can also be helpful, especially if a process has encountered an error, fails to exit or does not respond to the INT signal.
To view all the available signals, use a command such as a kill -l
Method #1 – The Killall Command
The first command you can use to kill Linux processes is the killall command. For this, you need the PID value or the name of the process you wish to terminate.
You can use a tool such as pgrep to get the PID of a specific process.
$ pgrep bash
Once you have the PID value of the process, you can terminate using the kill command as:
$ killall -15 2500
$ killall -15 bash
The command above will send a graceful termination signal.
To send a KILL signal, use the -9 integer as:
$ kill -9 2500
$ killall -9 bash
How to kill all user processes with killall
Killall command allows you to terminate all the processes owned by a specific user. To do this, use the -u flag.
For example, to terminate all processes spawned by the ubuntu user.
$ sudo killall -u ubuntu
CAUTION: Depending on the user specified in the command, terminating the processes can cause the system to crash or stop critical system processes. Therefore, before applying this command, ensure the user in question is not running processes you do not wish to terminate.
Method #2: The Pkill Command
Another great tool you can use to terminate Linux processes is the pkill command. This command works similarly to killall.
For example, to terminate the bash process with pkill, use the command:
$ pkill -15 bash
$ pkill -15 2500
To kill all processes owned by the ubuntu user, we can use the command:
sudo pkill -9 -u ubuntu
That’s it for this one.
In this guide, we quickly discussed ways to terminate processes in Linux and various types of kill signals. As seen, killing a single process or multiple processes owned by a specific Linux user is easy.