Linux kill command
The kill
command in Linux is used to send signals to processes. By default, it sends the SIGTERM
(terminate) signal to a process, but you can specify other signals (such as SIGKILL
) to control the behavior of processes. The kill
command is primarily used to terminate a process, but it can also be used to send other types of signals to processes.
Basic Syntax:
<pid>
: The process ID (PID) of the process you want to send a signal to.
Default Behavior:
By default, kill
sends the SIGTERM
signal, which politely asks the process to terminate.
Commonly Used Signals with kill
:
SIGTERM (15)
: Terminate the process (default signal sent bykill
).SIGKILL (9)
: Forcefully terminate the process. This cannot be ignored by the process.SIGINT (2)
: Interrupt a process, similar to pressingCtrl + C
in the terminal.SIGSTOP (19)
: Stop (pause) a process. This can be resumed later usingSIGCONT
.SIGCONT (18)
: Resume a paused process.SIGHUP (1)
: Hang up the process, often used to reload a process (such as a daemon).
Example Usage:
Terminate a Process (SIGTERM): To send the default
SIGTERM
signal to a process with PID 1234:Sample Output (in the terminal):
- The process with PID 1234 will be asked to terminate gracefully.
Forcefully Kill a Process (SIGKILL): If the process does not terminate after receiving
SIGTERM
, you can forcefully kill it usingSIGKILL
(signal 9). This will immediately stop the process, regardless of its state.Sample Output (in the terminal):
- The process with PID 1234 is immediately killed without any cleanup.
Interrupt a Process (SIGINT): To simulate pressing
Ctrl + C
in the terminal to interrupt a running process, use theSIGINT
signal.Sample Output (in the terminal):
- The process with PID 1234 will be interrupted, and the process will terminate if it handles
SIGINT
.
- The process with PID 1234 will be interrupted, and the process will terminate if it handles
Pause a Process (SIGSTOP): To stop a running process, you can send the
SIGSTOP
signal. This pauses the process and it can be resumed later.Sample Output (in the terminal):
- The process with PID 1234 is paused and will not consume CPU until resumed.
Resume a Process (SIGCONT): After pausing a process with
SIGSTOP
, you can resume it withSIGCONT
.Sample Output (in the terminal):
- The process with PID 1234 resumes execution.
Kill Multiple Processes:
You can send a signal to multiple processes at once by specifying multiple PIDs.
Sample Output (in the terminal):
- This command will send the
SIGKILL
signal to processes with PIDs1234
,5678
, and91011
.
Using killall
:
The killall
command can be used to kill processes by name rather than by PID. This is useful if you want to terminate all processes with the same name.
For example, to kill all processes with the name firefox
:
Sample Output (in the terminal):
- All processes with the name
firefox
are terminated.
Displaying Available Signals:
You can list all available signals that can be used with kill
by using the -l
option.
Sample Output:
- This will display a list of signals that can be used with
kill
.
Conclusion:
The kill
command is a powerful tool to manage processes in Linux by sending signals that can terminate, pause, or interrupt processes. You can use it to gracefully terminate processes with the default SIGTERM
signal, or forcefully terminate them with SIGKILL
. By combining it with other signals like SIGSTOP
and SIGCONT
, you can control processes effectively for various use cases, including pausing and resuming processes.