How to Kill a Process in Linux: Terminate Processes with kill and killall Commands

How to Kill a Process in Linux: Terminate Processes with kill and killall Commands

When managing a Linux server, you might face unresponsive or malfunctioning processes that consume excessive system resources, slow down your work, and negatively impact other running processes. Knowing how to kill such a process in Linux is essential for efficient system administration.

This article will guide you through various methods to end unresponsive processes in Linux. Whether you’re a beginner or an experienced administrator, these methods will help you effectively manage processes in a Linux environment.


The kill Linux command terminates a running, unresponsive, or malfunctioning process in a controlled and secure manner.

How to Locate a Process in Linux

Before terminating a process, it’s crucial to identify it correctly. Luckily, the Linux system provides various tools for this purpose. The ps and top commands are among the most common tools.

The ps command displays a snapshot of all the running processes, allowing you to find a specific process’s ID (PID). Here’s a basic format of the ps command:

ps [options]

Here are the most common options to combine with the ps command:

  • -a – show processes for all users.
  • -u – display the current user’s processes.
  • -x – include processes without a terminal.

To make your list more specific, add a grep command. For example, the following command will display all running Java processes:

ps -aux | grep java

On the other hand, the top command offers a dynamic view of all Linux running processes and their resource usage.It doesn’t require additional arguments or options for primary use.

When ps and top aren’t specific enough, pidof and pgrep offer more targeted search capabilities.

The pidof command finds the PID of a process by its name. Here’s the basic format of the command:

pidof [options] [processname]

For instance, pidof nginx finds the PID of NGINX processes. A few options can be included, such as:

  • -c – ensures that only PIDs from the current root directory are returned.
  • -o – omits the specified PIDs from the results.
  • -s – returns only a single PID, typically the oldest, among the matching processes.

The pgrep command offers a more flexible search mechanism by name or other attributes. It uses the following format:

pgrep [options] [pattern]

For example, pgrep -u username locates processes run by a specific user. You can add these options to filter the output:

  • -n – returns only the newest instance among the matching processes.
  • -o – returns only the oldest instance among the matching processes.
  • -u – matches processes that the specified user owns.

kill Command Signals

After identifying which process to terminate, the next step is to send an appropriate signal using the kill command. Each signal type serves a different purpose, affecting how a process ends.

To view all available signals, use the following command:

kill -l

This lists all Linux process signal types, including:

  • SIGTERM (15) – this is the default and safest way to kill a running process in Linux. It allows the process to terminate gracefully.
  • SIGKILL (9) – this signal immediately stops any primary or background process without allowing it to clean up.
  • SIGSTOP (19) – pauses a current process without terminating it.
  • SIGHUP (1) – this signal reports that the user’s terminal is disconnected, often leading to the process’s termination.
  • SIGINT (2) – this signal interrupts a process, typically sent from the keyboard shortcut Ctrl+C.

When using kill signals, you can specify the signal name or its numeric value. It’s crucial to choose the right kill signal based on the scenario and the impact you want on the process.

For instance, the SIGTERM signal is preferred for safe process control in Linux, allowing processes to save their state and exit cleanly. SIGKILL, while practical, should be sparingly used as it may lead to data loss or corruption.

How to Kill a Process in Linux

After understanding the signals, let’s explore the methods to kill a process running in Linux. If you use a virtual private server (VPS), access the server with an SSH client, such as PuTTY.

Hostinger VPS customers can use our built-in browser terminal for efficient process termination. Available for all VPS hosting plans, this tool provides a convenient way to manage your server directly from the browser.

How to Kill a Process Using Hostinger VPS

Here’s how to kill a process via hPanel:

  1. Log in to hPanel and navigate to the VPS section.
  2. Select the server you wish to manage and access the SSH access tab. You will find the SSH username and password needed to enter the terminal.
Finding SSH credentials on the SSH access tab of the hPanel's VPS dashboard
  1. Click on the Browser terminal button. This will open a new terminal window in a new tab.
  2. Enter the server by typing in the SSH credentials you have obtained.
Entering SSH credentials to access the server on Hostinger's browser terminal

After logging in, you can use the kill command to terminate processes. For instance:

kill 63772

63772 is the PID of the process you want to quit. You can also use other methods provided in the following sections on Hostinger’s browser terminal.

Once done, access the floating Hide/Show the control bar button and click Disconnect to stop the connection.

Accessing the Disconnect option from the floating control bar button on Hostinger's browser terminal

Suggested Reading

Learn how to choose the best VPS provider to make an informed decision.

How to Kill a Process Using the kill Command with a PID

Killing a single process in Linux is simple when you know its PID. Execute the kill [PID] command, for example:

kill 12345

This will send a SIGTERM to the process with PID 12345, attempting to terminate it gracefully. It is beneficial to kill a background process in Linux.

If the process does not terminate with the default SIGTERM, you might need to kill the process forcefully. This is done by adding the -9 option, which sends a SIGKILL signal. For instance:

kill -9 12345

How to Kill Multiple Processes

You can kill multiple processes at once in Linux using the same kill command. Pass multiple PIDs to the command, for instance:

kill 12345 67890 13579

This simultaneously sends a termination signal to the process IDs 12345, 67890, and 13579.

In scenarios where you need to kill processes with similar characteristics, wildcards can be used with commands like pgrep. For example:

kill $(pgrep pattern)

This command will kill all the processes that match the given pattern.

How to Kill a Process Using the pkill Command

The pkill command in Linux offers a more flexible approach for killing processes by name or other attributes. It is advantageous when you don’t know a specific PID.

To forcefully kill a process by name in Linux, run pkill [process name]. For example:

pkill apache

This command will terminate all the processes containing the name apache.

Additionally, you can employ the following options with pkill:

  • -u [username] – kills processes owned by a specific user.
  • -t [terminal] – kills processes attached to a specific terminal.
  • -l – provides a detailed process list along with the PID.

Please exercise caution when using pkill, as it can affect multiple processes. For example, if you have processes named apache and apache-helper, running pkill apache will terminate both. Always ensure you’re targeting the exact process name to avoid unintended disruptions.

How to Kill a Process Using the killall Command

The killall command is similar to pkill but has a distinct method in Linux process termination.

While pkill is effective for killing processes based on a partial match of the process name, the killall command requires the exact match name of the process. This makes killall more specific and less likely to terminate unintended processes.

To use this command, type killall [process name]. For instance:

killall apache

It will terminate all processes named apache. It is specifically handy to kill background processes simultaneously.

The killall command can be customized with timestamp options, including:

  • -o [time] – only kills processes older than the specified time.
  • -y [time] – only kills processes newer than the specified time.

The [time] specification in these options is usually given in the format of s for seconds, m for minutes, h for hours, d for days, w for weeks, M for months, and y for years.

For example, this command terminates all currently running processes named chrome that have been running for longer than 30 minutes:

killall -o 30m chrome

Conclusion

In this guide, we’ve explored various methods to kill a process in Linux using a shell script. We’ve covered different signals and the pkill, killall, and kill commands. These techniques are crucial for facilitating a graceful shutdown in Linux and avoiding system instability.

Mastering these skills is vital for managing background processes confidently and precisely, which is key to effective Linux task management.

How to Kill a Process in Linux FAQ

This section answers the most common questions about how to force-kill a process in Linux.

What Processes Can You Kill in Linux?

In Linux, you can terminate most processes, including parent and child processes. However, caution is advised, especially with system processes and those initiated by the root user. Closing a critical parent process may lead to system failure or unexpected behavior.

What Is the Difference Between the kill and killall Commands?

The Linux kill command is used to forcefully end a process in Linux using its PID, while the killall command terminates all processes with a specific name. The above commands are integral parts of Linux system administration.

How Do I Find the Process ID (PID) of a Process I Want to Kill?

To find a PID in the Linux operating system, use the ps command, which displays the process table. Alternatively, pgrep [process name] can locate PIDs of processes by name. This is crucial, especially for identifying processes owned by a different user that must be successfully killed.

Author
The author

Ariffud Muhammad

Ariffud is a Technical Content Writer with an educational background in Informatics. He has extensive expertise in Linux and VPS, authoring over 200 articles on server management and web development. Follow him on LinkedIn.