kill / pkill / killall
| Since: | kill / killall | All Linux |
|---|---|---|
| macOS(2001 Cheetah) | ||
| Bash 1.0(1989) | ||
| pkill | All Linux | |
| macOS(2007 Leopard) | ||
| Bash 1.0(1989) |
kill sends a signal to a process specified by its process ID (PID). pkill sends a signal by process name, and killall sends a signal to all processes whose name matches exactly. These commands are used to stop, restart, or reload the configuration of processes.
Syntax
Use kill to send a signal by specifying a PID.
kill PID kill -signal_name PID kill -signal_number PID
Use pkill to search by process name and send a signal.
pkill process_name pkill -signal process_name pkill -u username process_name
Use killall to send a signal to all processes with an exactly matching name.
killall process_name killall -signal process_name
List available signals.
kill -l
Common Signals
| Signal | Number | Description |
|---|---|---|
| SIGTERM | 15 | Requests graceful termination (default). The process can clean up before exiting. |
| SIGKILL | 9 | Forces immediate termination. The process cannot ignore this signal and exits without cleanup. |
| SIGINT | 2 | Interrupt signal, equivalent to pressing Ctrl+C. |
| SIGHUP | 1 | Hangup signal. Many daemons use this to reload their configuration files. |
| SIGSTOP | 19 | Pauses a process (cannot be ignored). |
| SIGCONT | 18 | Resumes a stopped process. |
| SIGUSR1/2 | 10/12 | User-defined signals for application-specific handling. |
Sample Code
The following examples assume that sleep 1000 is running with PID 12345.
ps aux | grep "sleep" | grep -v grep alice 12345 0.0 0.0 5536 1024 pts/0 S 10:00 0:00 sleep 1000
Request graceful termination with SIGTERM. When no signal is specified, the default SIGTERM (15) is sent.
kill 12345
Use SIGKILL (-9) to force-terminate an unresponsive process. The process stops immediately without any cleanup.
kill -9 12345 kill -SIGKILL 12345 # same effect
Send SIGHUP to nginx to reload its configuration file. Most daemons reload their config when they receive this signal.
kill -HUP $(cat /var/run/nginx.pid)
Use pkill to stop a process by name, saving you the trouble of looking up its PID.
pkill sleep # send SIGTERM to processes whose name contains "sleep" pkill -9 myapp # force-terminate pkill -u www-data php-fpm # stop a process owned by a specific user
killall sends a signal to all processes whose name matches exactly.
killall nginx
Use kill -0 to check whether a process exists without sending an actual signal. This is useful for monitoring processes in scripts.
sample_check_process.sh
kill 12345 && echo "Signal sent successfully"
sleep 1
if ! kill -0 12345 2>/dev/null; then
echo "Process has terminated"
fi
Run the following command:
bash check_process.sh Signal sent successfully Process has terminated
Example of capturing a background process PID and stopping it.
sample_kill_bg.sh
sleep 100 & bg_pid=$! echo "Background PID: $bg_pid" kill "$bg_pid" echo "Stop signal sent"
Run the following command:
bash kill_bg.sh Background PID: 12348 Stop signal sent
Common Mistakes
Common Mistake 1: Sending SIGKILL before trying SIGTERM
SIGKILL cannot be caught or ignored, so the process has no chance to clean up. Sending it first can cause data corruption or leave lock files behind.
kill -9 12345 (process has no chance to flush data or release resources)
Send SIGTERM first and wait for the process to exit gracefully. Use SIGKILL only if it does not respond.
kill 12345 sleep 3 kill -0 12345 2>/dev/null && kill -9 12345
Common Mistake 2: pkill pattern matches unintended processes
pkill matches any process whose name contains the pattern. A short pattern can match processes you did not intend to kill.
pkill php (may kill php-fpm, php-cgi, and any other process with "php" in the name)
Use -x to match the full process name exactly.
pkill -x php
Notes
The recommended practice is to first send SIGTERM (-15) to give the process a chance to exit gracefully, and only use SIGKILL (-9) if the process does not terminate. SIGKILL skips flushing open files and releasing resources, which can result in data corruption.
$ kill -0 PID only checks whether the process exists without sending any signal (exit status 0 = exists, non-zero = does not exist). This is useful for process monitoring in scripts.
To inspect processes, see ps / top / htop. For running processes in the background, see & (background execution).
If you find any errors or copyright issues, please contact us.