How to Kill Processes in Ubuntu? | Comprehensive Guid
Killing Processes essentially means stopping their execution instantly without allowing them to execute the closing protocols.
In Ubuntu and other Linux Distributions, there comes a time when a certain command or application needs to be closed manually. This is often true when a command or application is not responding or consuming too many resources. This guide will show you the step-by-step procedure for killing processes in Ubuntu and the different commands that can be used for this purpose.
Table of Contents
Let’s get started!
How to Kill Processes in Ubuntu?
To Kill a Process in Ubuntu, you must first find its process ID (PID). After that, use a kill command of your choice while specifying the termination signal to be sent to the process. Alternatively, you can use the GUI tool System Monitor to kill the process of your choice.
There are quite a few built-in commands that help you locate the process ID of a process and, similarly, a few commands that allow you to kill the process. However, the main thing is the signal sent to the process. Based on the signal, you can use the same command to stop, kill, or continue a process’s execution.
Let’s discuss the different steps for killing a process in order.
Locate the Process’s ID (PID)
The first step is to locate the ID of the process or application that you want to kill. As mentioned earlier, multiple Ubuntu commands can help you find the PID. Let’s go over some of such commands.
1. The “ps aux” Command
The ps aux command displays the process IDs of all processes running on your Ubuntu machine, including the root processes. To use this command, simply open up a new terminal (CTRL + ALT + T), type “ps aux,” and hit the “Enter” key. Upon executing, you will see the following output on the terminal session.
As you can see from the screenshot, it starts the processes from PID 1 till the very last process. Find the PID of the process you want to kill from the list. However, this is a long list, and scrolling through this list is not a smart way. Therefore, you can choose to go for another command. The “ps aux” command is useful when you want to find the PID of system processes.
2. The “pidof” Command
The second command, “pidof,” takes in the name of the application whose process ID you want to find and returns all the IDs of this application’s subprocesses. To demonstrate the working of this command, let’s try to find the ID of “firefox.” Open up a new terminal and type the following command inside it.
pidof firefox
When you execute the above command, you will get the following output displayed on the terminal session.
As you can see from the output image, the application Firefox has multiple PIDs because It is running multiple subprocesses. If you want to kill the entire tree of Firefox, you will use the lowest ID because the lowest PID is the parent ID of that application.
3. The “pgrep” Command
The third command you can use to find the process ID is the “pgrep” command. This command is used the same exact way as the “pidof” command, with the only difference being that it doesn’t return the PIDs of subprocesses. It only returns the PID of the parent process of the specified application/process.
To use this command to find Firefox’s process ID, open a new terminal session and type the following command inside.
pgrep firefox
You will get the following output on the terminal when you execute the command.
You can see that the “pgrep” command has returned only one ID, Firefox’s root process ID. However, if the application runs multiple trees, the “pgrep” command will return the root process ID for each tree.
4. The “ps” and “grep” Command
In this method, you will get the output of the “ps aux” command and filter it out using the “grep” command to find the ID of your desired application/process. For example, if you want to find the process ID of firefox, use the following command.
ps aux | grep -i “firefox”
Running this command will result in the following output on the terminal session.
The process ID is displayed in the second column of the output, which, in this case, is 5287.
Kill the Process Using Kill Commands
Once you have figured out the process you want to kill, the next step is simply using a kill command and sending a termination signal to a specific PID. Let’s look at some commands and their methods.
1. The “kill” Command
The first command you can use to kill processes in Ubuntu is the Kill command. The Kill command is used to send different signals to a given process. But for now, let’s focus on the signal flag “-9”, used for forceful termination.
To kill a process using the kill command, use the following command template.
kill -9 {process ID}
To demonstrate this, let’s try killing the process “firefox” using the following command.
kill -9 5407
When you execute the command, the terminal will not display any confirmation message.
However, make sure to use the PID that you have on your machine. As you can see, the command “pidof” has been used to find the process ID of firefox. Anyhow, the next step is to verify that the process has been terminated. To do this, again run the “pidof firefox” command.
As you can see in the output image, the “pidof” command didn’t return any process ID. This ensures that the process has been successfully killed.
As mentioned earlier, the kill command can send different signals to the process. These signals are as follows.
- The “-1” Signal is used to hang up the processes of a specified program/application.
- The “-9” is used to forcefully terminate a process without letting it perform the exit/shutdown protocol.
- The “-15” sends a termination signal to a process. However, it allows the process to execute the exit code and close independently.
- The signals “-17,” “-19,” and “-23” are used to stop the execution of the program rather than killing it.
All of these signals are useful in different scenarios. You should decide which signal best suits your needs.
2. The “killall” Command
While the kill command can kill a specific process using its process ID, it still leaves the parent process running if the specified PID is a subprocess. However, to tackle this, you can use the “killall” command to send a “kill” signal to all subprocesses of a specified application.
The syntax for using the “killall” command is represented as.
killall {ProcessName/ApplicationName}
As you can see from the syntax, you do not need a specific process ID to use the killall command, as it terminates the entire process tree. You only need to provide the application name within the command. Let’s try killing the application spotify using the following command to demonstrate this.
killall spotify
After running the command, let’s use the “pidof spotify” command to ensure that all of the processes of spotify have been terminated.
The output confirms that all of the subprocesses of Spotify have been terminated using the “killall” command.
The “pkill” Command
The pkill command works the same way as the “killall” command and terminates a given application’s processes and subprocesses. The syntax for using the pkill command is as follows.
pkill {pattern}
The pattern is a regex pattern, which can be the name of the process itself. If you want to kill firefox using the pkill command, you will use the following command in the terminal.
pkill fire
As you can see from the output, the firefox process was terminated. However, you didn’t specify the entire name in the pkill command. This is because the pkill command takes a matching pattern. Therefore, any process with “fire” in its name has been terminated.
How do you kill all the processes of a given user in Ubuntu?
To kill all processes of a specific user in Ubuntu, you can use the “pkill” command or the “killall” command with the flag “-u”. The flag “-u” represents a specific user in your Ubuntu machine, and the command will kill all processes with the same user.
Suppose you want to kill the process of the user named “User1”, then you will simply run the following command in the terminal.
pkill -u User1
Alternatively, to use the killall command for the same purpose, you must use the following command inside the terminal.
killall -u User1
However, this command will only work through users added to the “root” groups. Also, be careful when running this command, as killing all processes also means killing system processes, which can cause the system to crash.
How to Kill Processes in Ubuntu Using the GUI?
To kill a process using the graphical user interface in Ubuntu, head over to applications and search for System monitor. Within the system monitor, you must find the processes you want to kill and select the kill or end option. You can also choose to stop its processing instead of killing it.
To demonstrate this, open up the firefox application on your Ubuntu machine. After that, click on the “Application” icon from the bottom left on your Ubuntu Desktop.
Within the application, search for “System Monitor” and open it.
Once inside the system monitor, head to the “Processes” tab and find the process you want to kill (Firefox).
After that, right-click on the process, and from the options, choose “End” or “Kill”.
After that, you will get a confirmation prompt that you are about to kill a specific process. Choose the option “kill process” to continue.
You have successfully terminated Firefox using the GUI method in Ubuntu.
Affordable VPS Hosting With Dracula Servers
Looking for reliable and budget-friendly Virtual Private Server (VPS) hosting? Look no further than Dracula Servers. Dracula Servers offers a range of VPS hosting plans tailored to meet diverse needs. With competitive pricing, robust performance, and a user-friendly interface, it’s an excellent choice for individuals and businesses alike.
Explore the Dracula Servers website to discover hosting solutions that align with your requirements and take your online presence to new heights with their affordable and efficient VPS hosting services.
Visit Dracula Servers and experience reliable VPS hosting without breaking the bank.
What is the Difference Between Killing a Process, Ending a Process, and Stopping a Process?
Whenever a process completes its processing, it runs its exit code, allowing it to safely close down all its sub-processes. Killing a process terminates that process without allowing it to run the exit code, which is why it is known as instant, abrupt, or forceful termination.
On the other hand, ending a process means sending a signal to the process to stop its processing and start executing the exit code. This is much better if you do not want to lose progress made by a certain process or application, as this allows the process to close safely.
Lastly, stopping a process means sending a signal to the process to stop its execution and sit idle for the continuation command. This is more like hanging up the process till further notice. A stopped process can be continued at any moment, but a killed or ended process cannot.
Wrap up
To Kill a process in Ubuntu, you can use command-line utilities or the GUI method. If you want to use the terminal, then the first step is to find the process ID (PID) of the application that you want to kill. Once you have found the process’s ID, the next step is to send a kill signal to this process using a kill command.
Such kill commands allow you to kill the process without requiring its PID. However, for such commands, you need to know the name of the process you want to kill or at least a part of the name that can be used as a pattern.
If you want to kill all of a user’s processes, you can use the pkill or the killall command while specifying the user with the “-u” flag.
Lastly, you can use the System Monitor, a GUI application on Ubuntu, to find and kill a specific application.
Check out More Linux Tutorials Here!