How to terminate a process using the kill command in Linux

In the world of Linux operating systems, managing processes is an essential skill for any system administrator. Processes, which represent tasks or applications running on the system, sometimes need to be stopped—whether they are hung, consuming too many resources, or simply started by mistake. To handle this, the kill command is the tool of choice.

The kill command in Linux allows you to terminate a process using its Process ID (PID). This functionality is particularly useful for those managing servers or VPS environments, as it provides precise control over running tasks. Below, we explore common scenarios where the kill command is necessary, the available signals, and how to effectively terminate processes.

When Is the Kill Command Useful?

There are multiple situations where using the kill command can be necessary:

  • Stopping automated processes.
  • Terminating an application started by mistake.
  • Ending a process that is consuming too many system resources.
  • Forcing the shutdown of an unresponsive process.
  • Stopping background processes that are no longer needed.

In addition to stopping processes, the kill command can also send specific signals to processes, offering greater control over their behavior.

How to Find a Process ID (PID)

To use the kill command, you first need to find the PID of the process you want to terminate. The ps command allows you to list all running processes along with their PIDs. For example:

ps

If you’re looking for a specific process, such as one related to Firefox, you can narrow the search using grep:

ps -ux | grep firefox

This will display only the Firefox-related processes, making it easier to find the relevant PID.

Listing Available Kill Signals in Linux

The kill command allows you to send a variety of signals to processes, with SIGTERM (15) and SIGKILL (9) being the most common. SIGTERM politely requests that the process terminates, while SIGKILL forces it to end immediately. To see all the available signals, use the following command:

kill -l

This will display a list of signals, along with their names and corresponding numbers, which can be used to control processes more precisely.

How to Terminate a Process in Linux Using the Kill Command

To terminate a specific process using its PID, the basic syntax for the kill command is:

kill <PID>

If no signal is specified, SIGTERM will be sent by default. For example:

kill 59123

If the process does not respond to SIGTERM, you can force its termination using SIGKILL:

kill -9 59123

Here, the number 9 represents SIGKILL, which ensures the immediate termination of the process.

Terminating Multiple Processes

If you need to stop several processes at once, kill allows you to specify multiple PIDs:

kill -9 pid1 pid2 pid3

For example:

kill -9 59123 38234 17345

This command will terminate all the specified processes.

Using the Pkill Command

Pkill is a variation of the kill command that allows you to terminate processes by name rather than PID. For instance:

pkill firefox

This will close all Firefox browser processes. If you only know part of the process name, pkill also supports partial matches:

pkill fire

However, use caution when using partial matches, as you might accidentally terminate the wrong processes. To verify the exact process name, you can use:

pidof firefox

This will provide the PID of all running Firefox processes.

Using the Killall Command

The basic difference between killall and kill is that killall can terminate processes by name, whereas kill uses PIDs. For example:

killall firefox

This is similar to pkill, but killall performs an exact name match, making it safer when terminating a specific process. Another key difference is that killall belongs to the psmisc package, while commands like ps, top, kill, and pkill belong to the procps package.

Additionally, killall can be customized to terminate processes based on their runtime. If you want to kill a process that has been running for less than 40 minutes, you can use:

killall -y 40m firefox

You can also specify time units for seconds, minutes, hours, days, weeks, months, or years using these options:

  • s – seconds
  • m – minutes
  • h – hours
  • d – days
  • w – weeks
  • M – months
  • y – years

Conclusion

In this tutorial, we covered the kill command and its most useful variations, such as pkill and killall. These tools are essential for managing processes in a Linux environment. For more information on the kill command, you can refer to the Linux manual:

man kill

With these commands in your toolkit, you’ll be able to effectively manage processes, ensuring optimal performance and resource management on your Linux system or server.

Scroll to Top