The kill
command in Linux is used to terminate or send signals to processes. While it's commonly associated with killing processes, it can also be used for other purposes such as pausing or resuming processes by sending specific signals.
kill [options] <pid>...
Where <pid>
is the Process ID of the process you want to interact with.
The simplest usage of kill
sends the SIGTERM (15) signal by default, which requests the process to terminate gracefully.
kill <pid>
kill 1234
This sends a SIGTERM to the process with PID 1234, asking it to terminate.
The kill
command allows sending different signals to processes. Here are some commonly used signals:
This is the default signal sent by kill
if no signal is specified. It requests the process to terminate gracefully.
kill 1234
This signal forcefully kills a process, immediately terminating it without allowing it to clean up or handle the signal.
kill -9 1234
- Use: When a process doesn't terminate after receiving SIGTERM, you can forcefully kill it using SIGKILL.
- Warning: This signal should be used with caution, as it can lead to data loss or corruption, since the process cannot handle cleanup.
This signal pauses a running process without terminating it. You can later resume it with the SIGCONT
signal.
kill -19 1234
- Use: Pausing a process temporarily.
Resumes a paused process (one that received SIGSTOP).
kill -18 1234
This signal interrupts a process, similar to pressing Ctrl+C
in the terminal. It asks the process to terminate.
kill -2 1234
Similar to SIGINT, but also causes the process to dump core, which can be useful for debugging.
kill -3 1234
Used to tell a daemon process to reload its configuration without terminating it. Common for services like Apache or Nginx.
kill -1 1234
- Use: When you want a process to re-read its configuration files.
These are custom signals that can be sent to processes to trigger specific actions defined by the process itself.
kill -10 1234
kill -12 1234
You can specify the signal to send using the -s
option followed by the signal name or number:
kill -s SIGKILL 1234
kill -s 9 1234
Both of the above commands are equivalent to kill -9 1234
, sending the SIGKILL signal.
You can send a signal to multiple processes by specifying multiple PIDs:
kill 1234 5678 91011
Use the pkill
command (a variant of kill
) to send a signal to processes based on name. For example, to terminate all firefox
processes:
pkill firefox
- You can also use
pkill
to send different signals, such aspkill -9 firefox
.
You can send a signal to all processes in a process group using kill
with the -g
option:
kill -g <pgid> <signal>
Example:
kill -9 -g 1234
This sends a SIGKILL
signal to all processes in the group with PGID 1234.
Before using kill
, you often need to know the PID of the process you want to terminate.
-
List all processes: Use the
ps
command to view all running processes and their PIDs.ps -e
-
Find a process by name: Use
pgrep
to search for a process by name and retrieve its PID(s).pgrep firefox
-
Show all processes in a tree format:
ps aux --forest
-
List processes with detailed information:
ps aux
kill 1234
- Terminates process 1234 by sending SIGTERM.
kill -9 1234
- Forcefully kills process 1234 with SIGKILL.
kill -19 1234
- Pauses process 1234 using SIGSTOP.
kill -18 1234
- Resumes process 1234 using SIGCONT.
kill -10 1234
- Sends SIGUSR1 (signal 10) to process 1234, which can trigger a user-defined action.
pkill
is a more flexible command than kill
because it allows you to send signals to processes based on their name, group, user, or other attributes.
pkill -9 firefox
This sends the SIGKILL signal to all processes named firefox
.
Similar to pkill
, killall
allows you to terminate processes by name. It is commonly used to kill all processes with a specific name.
killall -9 firefox
- SIGTERM (15) is the default signal and is preferred for graceful termination, allowing processes to clean up resources.
- SIGKILL (9) should be used as a last resort when a process does not respond to SIGTERM.
- Always verify that you’re terminating the correct process using
ps
orpgrep
before sendingSIGKILL
, as it may cause unintended consequences (e.g., data loss, system instability).
Let me know if you'd like additional examples or further details!
The kill
command in Linux is used to terminate processes by sending specific signals to them. It is a vital tool for process management, allowing you to end misbehaving or unneeded processes.
The kill
command sends a signal to a process. By default, it sends the SIGTERM
signal, which politely requests the process to terminate. If a process doesn’t respond, you can use stronger signals like SIGKILL
.
kill [options] <pid>
<pid>
: Process ID of the target process.- Signals: Used to specify how the process should be handled (e.g., terminate, stop, restart).
Signal | Number | Description |
---|---|---|
SIGTERM |
15 | Politely asks a process to terminate. Default signal. |
SIGKILL |
9 | Forcefully terminates a process. Cannot be ignored. |
SIGHUP |
1 | Restarts the process (common for daemons). |
SIGSTOP |
19 | Pauses (stops) a process. |
SIGCONT |
18 | Resumes a paused process. |
SIGUSR1 |
10 | User-defined signal 1. |
SIGUSR2 |
12 | User-defined signal 2. |
You need the PID of the process to use kill
. Use ps
, top
, or pgrep
to find it:
ps -ef | grep process_name
To terminate a process gracefully:
kill <pid>
Example:
kill 1234
If the process doesn’t respond to SIGTERM
:
kill -9 <pid>
Example:
kill -9 1234
Some daemons and services restart when sent SIGHUP
:
kill -1 <pid>
- Pause a process with
SIGSTOP
:kill -19 <pid>
- Resume the process with
SIGCONT
:kill -18 <pid>
The pkill
command allows you to kill processes by name instead of PID:
pkill process_name
Example:
pkill firefox
To terminate all processes with a specific name:
killall process_name
Example:
killall apache2
You can send specific signals by name or number:
kill -<signal_number> <pid>
or
kill -<signal_name> <pid>
Example:
kill -SIGUSR1 1234
or
kill -10 1234
You can specify multiple PIDs to terminate:
kill 1234 5678 91011
Find and kill a process in one command:
ps -ef | grep process_name | awk '{print $2}' | xargs kill
Signal | Description |
---|---|
SIGTERM |
Allows the process to clean up and exit gracefully. |
SIGKILL |
Immediately kills the process. Cannot be intercepted. |
SIGHUP |
Often used to reload configuration files or restart daemons. |
SIGINT |
Sent by pressing Ctrl+C in the terminal. |
SIGQUIT |
Sent by pressing Ctrl+\ . Generates a core dump. |
SIGSTOP |
Stops (pauses) the process. |
SIGCONT |
Continues a stopped process. |
SIGUSR1/2 |
User-defined signals, typically used for custom actions. |
pkill -u username
Zombie processes have a status of Z
. Find and kill them:
ps -eo pid,stat,cmd | grep Z | awk '{print $1}' | xargs kill -9
If you’ve started a process in the background (&
), use jobs
to find it:
jobs
kill %<job_number>
Example:
kill %1
For processes that support SIGHUP
:
kill -1 <pid>
- Process is stuck in an uninterruptible state (e.g., waiting for I/O). You must resolve the underlying issue (e.g., file system issue).
- Permission Denied: Ensure you have sufficient privileges (use
sudo
if necessary):sudo kill <pid>
Use ssh
to connect and then use kill
:
ssh user@remote-server
kill <pid>
Command | Description |
---|---|
kill |
Requires a specific PID to terminate a process. |
pkill |
Terminates processes by name. |
killall |
Terminates all processes matching a name. |
Let me know if you want more examples or need help applying the kill
command in specific scenarios!