The kill
command in Linux is used to terminate processes by sending specific signals. By default, it sends the SIGTERM
signal (signal 15), which gracefully stops a process. However, other signals, like SIGKILL
(9), can forcefully terminate a process when necessary.
Terminate a Process by PID
kill 1234
Sends the default SIGTERM
signal to gracefully terminate process 1234
.
Forcefully Kill a Process
kill -9 1234
-9
sends theSIGKILL
signal, which forcefully terminates the process.
Reload a Process (e.g., Web Server)
kill -1 1234
-1
sends theSIGHUP
signal, often used to reload configurations (e.g., Nginx, Apache).
Stop a Process Temporarily
kill -19 1234
-19
sends theSIGSTOP
signal, pausing the process.
Resume a Stopped Process
kill -18 1234
-18
sends theSIGCONT
signal, resuming a paused process.
List All Available Signals
kill -l
Displays a list of all signals (e.g., SIGTERM
, SIGKILL
, SIGHUP
).
Kill a Process by Name
pkill firefox
Terminates all processes named firefox
.
Kill Processes by User
pkill -u username
Terminates all processes owned by username
.
Kill Processes Matching a Pattern
pkill -f "python script.py"
-f
: Matches the full command line (e.g., killspython script.py
).
Send a Custom Signal
kill -SIGUSR1 1234
Sends the SIGUSR1
signal, often used for custom process handling.
Kill All Processes in a Process Group
kill -- -12345
-- -12345
: Sends the signal to all processes in the process group12345
.
Kill Background Jobs
kill %1
Terminates the background job with ID 1
(use jobs
to list background jobs).
Gracefully Stop Multiple Processes
kill 1234 5678 91011
Sends SIGTERM
to processes 1234
, 5678
, and 91011
.
Verify if a Process Was Killed
kill -0 1234
-0
checks if the process exists without sending a signal.- Returns
0
if the process exists,1
if it doesn’t.
Kill All Processes Except the Current Shell
kill -9 -1
-1
: SendsSIGKILL
to all processes except the current shell.- Warning: Use with caution—this can crash the system.
Key Notes:
- Signals: Use
kill -l
to list all signals. Common ones include: SIGTERM
(15): Graceful termination.SIGKILL
(9): Forceful termination.SIGHUP
(1): Reload configurations.- Permissions: Only the root user or process owner can kill processes.
- Alternatives: Use
pkill
orkillall
to target processes by name.