Ways to Kill Ubuntu Processes

This article looks at several ways to kill a process in Ubuntu using specific terminal commands that send interrupts to an application.

Ubuntu

If you’ve ever experienced a frozen application in Linux or had trouble closing an application, you know that it’s impossible to shut down the process normally. Zombie processes are another frustration that takes up unnecessary resources. A zombie process is one that completed its function but wasn’t removed from the process table, either due to a bug or a parent or child process that terminated abruptly without cleanup. To free up resources and terminate hung applications, you can kill the process behind it. Ubuntu is a flavor of Linux, so you can kill a process in Ubuntu using specific terminal commands that send interrupts to an application.

What Are Ubuntu Signals?

To control processes and interrupts, the Linux kernel supports signals. Signals tell an application that the user or another program sent a command. When a program receives a signal, what happens next depends on the type of signal. Some signals expect user feedback, but others are outside of the user’s control. Usually, signals outside of the user’s control involve errors with the application or computer hardware.

Before you kill a process with a signal, you need to get its process ID. You can get the process ID using the pidof command in Ubuntu. You need the name of the application or the full path to its executable. 

Here’s an example of using the command to get the ID for the VLC program:

The result will be a number that you can use to kill the process.

Another option is to use pgrep. The pgrep command will give a list of process IDs for any process containing the input string. For example, the following command will get a list of process IDs containing “systemd” in the name:

How to Kill an Ubuntu Process with SIGTERM (Signal 15)

SIGTERM is called a soft kill because the application can choose to ignore the request or close gracefully with cleanup procedures. Because it can allow the process to terminate gracefully, it’s the default kill signal. Child processes remain open, so you can still work with them even if the parent process has been terminated. If you send SIGTERM to a child process, it can let the parent process know that it’s terminating.

The following is an example of using SIGTERM on process 2432:

Notice that the SIGTERM command does not have any optional parameters like the SIGKILL interrupt.

How to Kill an Ubuntu Process with SIGKILL (Signal 9)

The SIGKILL command tells an application to close immediately without performing any cleanup procedures. It’s a last resort, and it should be used if you get no response from an application and need to free up resources. When you send SIGKILL to a process, it’s forced to close and cannot ignore the command. Any child processes are also killed, but if you kill a child process, you could create a zombie process since the child process is unable to send a message to the parent process that it’s terminating.

For example, an application might freeze and you no longer get responses from your commands, so using SIGKILL will close it and any child applications. The following command sends a SIGKILL interrupt:

In this example, the process ID is 2432. The application matching process ID 2432 will immediately close.

How to Kill a Zombie Process by PID

Before you can kill a zombie process, you first need its process ID. There could be several zombie processes and you may not realize it. Killing zombie processes could have unexpected unwanted issues, so use the kill command as a last resort. Review the parent process before killing a zombie process. You might find that it’s best to leave it in the process table and wait for the next maintenance period to avoid unexpected issues. Before using the kill command, the SIGCHLD command triggers the parent process to retry reading the zombie process and clean it up. The following command uses SIGCHLD to signal the parent process:

The following command displays all zombie processes:

Any process with a status of “Z” is a zombie process. The PID column displays its process ID. You need this ID, but you use it to get the parent process ID to properly remove the parent and child so that you don’t create additional zombie processes.

The following command gets the parent process ID for the process 2432:

The output will be the parent process ID. Suppose that the parent process ID is 8374. The following command will kill the parent and zombie process:

How to Kill All Processes with killall

Sometimes you don’t want to kill a specific process, but you want to kill an application process by name. It’s possible to have the same process open multiple times, so the killall command terminates all instances of a specific application. For example, you might have several Chrome browsers open. Using the killall command will close all of them. In contrast, the kill command would close one of them.

The following command kills all Chrome processes:

When and How to Use pkill

The pkill command is an optional way to kill a single process using its name. It’s an alternative to using the kill command to terminate a process by a process ID. Because it takes a name as a parameter, it’s preferred and easier to use than killing processes by ID.

The following is an example of the pkill command:

What to Do When You Can’t Kill a Process

Processes can ignore SIGTERM, but they cannot block SIGKILL. If there’s no response after using SIGTERM, try SIGKILL. Keep in mind that it can take some time for the operating system to deliver the signal to the application. Wait a few seconds before you determine that the process cannot be killed. At the very worst, you might need to reboot the system to free up resources.