In the world of Linux, processes can receive signals and react to them. This is one way we can interact with running programs. The kill command is a powerful tool that allows us to send a variety of signals to a running process.

While the name “kill” might suggest that its main purpose is to terminate a program, it can actually do much more. Let’s take a closer look at how to use the kill command.

Basic Usage

The basic usage of the kill command is as follows:

kill <PID>

By default, this command sends the TERM signal to the process identified by the specified process ID (PID). The TERM signal instructs the process to terminate gracefully.

Sending Different Signals

The kill command also supports various flags that allow us to send different signals to a process. Here are some commonly used flags:

  • kill -HUP <PID>: Sends the HUP signal, which stands for “hang up.” This signal is automatically sent when a terminal window that started a process is closed before terminating the process.

  • kill -INT <PID>: Sends the INT signal, which is the same signal generated when we press ctrl-C in the terminal. Typically, this signal terminates the process.

  • kill -KILL <PID>: Sends the KILL signal, not to the process, but to the operating system kernel. This signal immediately stops and terminates the process.

  • kill -TERM <PID>: Sends the TERM signal, which is the default signal sent by kill. The process will receive it and terminate itself gracefully.

  • kill -CONT <PID>: Sends the CONT signal, which can be used to resume a stopped process.

  • kill -STOP <PID>: Sends the STOP signal, not to the process, but to the operating system kernel. This signal immediately stops the process without terminating it.

You may also come across numeric values instead of signal names in some cases. For example, kill -1 <PID> is equivalent to sending the HUP signal, kill -2 <PID> is equivalent to sending the INT signal, and so on.

Compatibility

The kill command works on Linux, macOS, Windows Subsystem for Linux (WSL), and any UNIX-like environment. It is a versatile tool that can be used across different platforms.

So, the next time you need to send a signal to a running process, remember the power of the kill command and the various signals it offers.

Tags: Linux commands, kill, signal handling, process management