kill
Command: Terminating Processes in Linux
Summary
The kill
command is a fundamental utility in Linux used to send signals to processes, most commonly to terminate them. Understanding how to use kill
effectively is crucial for system administration and troubleshooting.
Introduction
The kill
command, despite its name, doesn't always "kill" a process. It sends a signal to a process, and the process's response depends on the signal it receives. The default signal sent by kill
is SIGTERM
, which politely requests the process to terminate. Processes can choose to ignore this signal, but most will terminate gracefully upon receiving it. Other signals can force a more immediate termination or trigger different behaviors within the process.
Use Case and Examples
Basic Termination
This command sends theSIGTERM
signal to the process with the specified Process ID (PID), requesting it to terminate. For example, kill 1234
will attempt to terminate the process with PID 1234. Sending a Specific Signal
These commands send theSIGKILL
signal (signal number 9) to the process with the specified PID. SIGKILL
is a non-catchable, non-ignorable signal that forces the process to terminate immediately. Use this signal as a last resort when SIGTERM
fails. Killing a Process by Name
This command usespgrep
to find the PIDs of all processes with the given name and then uses kill
to send SIGTERM
to those processes. For example, kill $(pgrep firefox)
will attempt to terminate all Firefox processes. Killing All Processes of a User
This command sends theSIGKILL
signal to all processes owned by the specified user. Be extremely careful when using this, as it can disrupt system services. Listing Signals
This command lists all available signals that can be sent using thekill
command along with their corresponding numerical values. Commonly used flags
Flag | Description | Example |
---|---|---|
-s <signal_name> | Specifies the signal to be sent. Uses the signal name (e.g., SIGKILL ). | kill -s SIGKILL 1234 (sends SIGKILL) |
-<signal_number> | Specifies the signal to be sent. Uses the signal number (e.g., 9 for SIGKILL ). | kill -9 1234 (sends SIGKILL) |
-l | Lists signal names. | kill -l (displays list of signals) |
-u <username> | Sends the signal to all processes owned by the specified user. | kill -9 -u john (kills all processes owned by user john) |