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
- Forcefully Kill a Process
- Reload a Process (e.g., Web Server)
- Stop a Process Temporarily
- Resume a Stopped Process
- List All Available Signals
- Kill a Process by Name
- Kill Processes by User
- Kill Processes Matching a Pattern
- Send a Custom Signal
- Kill All Processes in a Process Group
- Kill Background Jobs
- Gracefully Stop Multiple Processes
- Verify if a Process Was Killed
- Kill All Processes Except the Current Shell
Terminate a Process by PID
kill 1234
Sends the default SIGTERM signal to gracefully terminate process 1234.
Forcefully Kill a Process
kill -9 1234
-9sends theSIGKILLsignal, which forcefully terminates the process.
Reload a Process (e.g., Web Server)
kill -1 1234
-1sends theSIGHUPsignal, often used to reload configurations (e.g., Nginx, Apache).
Stop a Process Temporarily
kill -19 1234
-19sends theSIGSTOPsignal, pausing the process.
Resume a Stopped Process
kill -18 1234
-18sends theSIGCONTsignal, 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
-0checks if the process exists without sending a signal.- Returns
0if the process exists,1if it doesn’t.
Kill All Processes Except the Current Shell
kill -9 -1
-1: SendsSIGKILLto all processes except the current shell.- Warning: Use with caution—this can crash the system.
Key Notes:
- Signals: Use
kill -lto 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
pkillorkillallto target processes by name.
