How to use the kill command in Linux to terminate processes by PID

Introduction

In the day-to-day of a system administrator or an advanced Linux user, it is common to encounter processes that consume too many resources, become stuck, or are simply no longer needed. In these cases, the most direct and safe way to stop them is by using the kill command, which allows sending a signal to a process identified by its identification number (PID). This article explains what a PID is, how the kill command works, what signals exist, and provides practical examples so you can apply this knowledge immediately and safely.

What is a process and its PID

In Linux, each running program is represented as a process. The kernel assigns each process a unique identifier called PID (Process ID). This number allows the system and the user to refer to a specific process without ambiguity. You can view the list of active processes with commands such as ps aux or top, where the PID appears in the first column. Knowing the PID is the first step to be able to interact with the process, whether to pause it, resume it, or terminate it.

The kill command

The basic syntax of kill is:

kill [options] 

If no signal is specified, kill sends by default the SIGTERM (15) signal, which requests the process to terminate in an orderly manner, releasing resources and saving its state if it is programmed to do so. If the process ignores SIGTERM, you can escalate to stronger signals, such as SIGKILL (9), which forces the kernel to terminate the process immediately without possibility of interception or cleanup.

Most used signals

  • SIGTERM (15): friendly termination request.
  • SIGKILL (9): forced termination, cannot be ignored.
  • SIGINT (2): simulates interruption with Ctrl+C.
  • SIGSTOP (19): pauses the process (can be resumed with SIGCONT).
  • SIGCONT (18): resumes a stopped process.

You can specify the signal either by its number or by its name (prefixed with -). For example, kill -9 1234 or kill -SIGKILL 1234 have the same effect.

How to find the PID of a process

Before using kill you need to know the PID. Some common ways are:

  • ps aux | grep process_name: filters the list of processes.
  • pgrep -f pattern: directly returns the PID that matches the pattern.
  • pidof program_name: useful when you know the exact executable name.
  • top or htop: interactive interfaces that show the PID in real time.

Combining these tools with kill allows creating shortcuts, such as kill $(pgrep -f firefox) to terminate all instances of Firefox.

Practical examples

  1. Terminate a process whose PID you know: kill 5678
  2. Force termination with SIGKILL: kill -9 5678
  3. Send an interrupt signal as if the user pressed Ctrl+C: kill -2 5678
  4. Pause a process and then resume it: kill -STOP 5678 followed by kill -CONT 5678
  5. Terminate all processes of a user: kill -9 -u username (requires privileges).

Precautions and best practices

Although kill is powerful, improper use can cause system instability or data loss. It is recommended:

  • Try first with SIGTERM before resorting to SIGKILL.
  • Verify that the PID corresponds to the correct process (avoid confusing similar numbers).
  • Use sudo only when necessary to terminate processes of other users or the system.
  • On production servers, consider using control scripts or service management systems (systemd, supervisord) instead of manually killing processes.

Conclusion

The kill command is an essential tool in the arsenal of any Linux user. Understanding how it works, what signals exist, and how to locate the PID of a process allows you to manage the system effectively and safely. With the examples and best practices presented in this article, you will be prepared to terminate processes by PID without putting the stability of your environment at risk.

This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .