The kill command in Linux is used to terminate processes manually by sending specific signals to them. It’s a vital command for managing and controlling processes, especially in cases where a process becomes unresponsive or needs to be stopped. While the name suggests termination, kill can send various types of signals, not just to stop a process but also to perform other actions such as pausing or continuing it.
kill [options] <PID>- PID: The Process ID of the process you want to send a signal to. You can obtain the PID using commands like
ps,top, orpgrep.
By default, the kill command sends the TERM signal (signal number 15), which requests the process to terminate gracefully. However, you can specify different signals to control the process in various ways. Signals can be specified either by name or by number.
- SIGTERM (15): Gracefully terminate the process (default signal). The process can catch this signal and clean up resources before exiting.
- SIGKILL (9): Forcefully kill the process immediately. The process cannot catch or ignore this signal.
- SIGHUP (1): Hang up signal, often used to reload configuration files without killing the process.
- SIGSTOP (19): Pause (stop) the process. Cannot be caught or ignored.
- SIGCONT (18): Resume a stopped process.
-
By Signal Name:
kill -SIGTERM <PID>
-
By Signal Number:
kill -9 <PID> # Sends SIGKILL (forceful termination)
kill -SIGTERM 1234 # Gracefully stop process with PID 1234
kill -9 1234 # Forcefully kill process with PID 1234
kill -SIGHUP 5678 # Reload configuration for process with PID 5678
kill -SIGSTOP 9102 # Pause process with PID 9102
kill -SIGCONT 9102 # Resume process with PID 9102Before using the kill command, you need the Process ID (PID) of the process you want to terminate or control. There are several ways to find the PID of a process:
-
Using
pscommand:ps aux | grep <process_name>
- This lists all processes and includes their PIDs.
-
Using
pgrepcommand:pgrep <process_name>
- This returns the PID(s) of the process based on its name.
-
Using
toporhtop:- Run
toporhtop, locate the process in the list, and note the PID.
- Run
| Signal | Number | Description | Use Case |
|---|---|---|---|
SIGTERM |
15 | Gracefully terminate the process | Default signal used by kill. Allows processes to clean up. |
SIGKILL |
9 | Forcefully kill the process | When a process doesn’t respond to SIGTERM. Cannot be ignored or handled by the process. |
SIGHUP |
1 | Reload configuration files | Often used to make services re-read their configuration files without restarting them. |
SIGSTOP |
19 | Stop a process | Pauses the process execution. The process can be resumed later with SIGCONT. |
SIGCONT |
18 | Resume a stopped process | Used to resume a process that was stopped by SIGSTOP. |
SIGINT |
2 | Interrupt the process (same as Ctrl+C) | Send an interrupt request to stop a running process. |
SIGUSR1 |
10 | User-defined signal 1 | A signal that can be used for custom purposes by applications. |
SIGUSR2 |
12 | User-defined signal 2 | Another custom signal for application-specific purposes. |
There are several commands related to kill that provide additional functionality:
-
killall:- The
killallcommand terminates all processes with the given name.
killall <process_name>
- Example:
killall nginx
- The
-
pkill:- The
pkillcommand sends a signal to processes based on their name, user, or other attributes.
pkill -9 <process_name>
- Example:
pkill -HUP apache2
- The
-
xkill(Graphical):- The
xkillcommand is used in graphical environments to kill misbehaving graphical applications. You can click on the window to close it.
xkill
- The
The kill command can send signals to multiple PIDs in one command:
kill -9 1234 5678 9102This will forcefully terminate the processes with the PIDs 1234, 5678, and 9102.
You can kill all processes running under a specific user using:
sudo pkill -u <username>This will terminate all processes owned by the specified user.
When running processes in the background using & or controlling processes via job control (Ctrl+Z, bg, fg), you can use kill with job numbers.
-
List all jobs:
jobs -
Kill a specific job by its job number:
kill %<job_number>
Example:
kill %1
To stop a service that is running as a process, you may want to terminate it gracefully:
kill -SIGTERM 1234This allows the process to complete ongoing tasks and clean up before exiting.
If a process is stuck or unresponsive, a SIGKILL signal can force it to terminate immediately:
kill -9 5678This is often used as a last resort when other signals fail to stop the process.
To reload the configuration of a web server (like Apache) without restarting it completely:
kill -SIGHUP 9102This will instruct the server to re-read its configuration files.
- Permission Issues: You can only send signals to processes that you own unless you have superuser (root) privileges.
- Misuse of
SIGKILL: WhileSIGKILLimmediately terminates a process, it doesn’t allow the process to clean up resources (e.g., temporary files, database connections). Use it only when necessary. - Avoid Killing Critical System Processes: Be cautious when killing processes, especially those related to critical system services (
init,systemd, etc.), as it can destabilize your system.
-
Basic Command:
kill <PID>
-
Kill Using Signal Name:
kill -SIGTERM <PID>
-
Force Kill:
kill -9 <PID>
-
Kill All Instances of a Process:
killall <process_name>
-
Kill Specific User Processes:
pkill -u <username>
-
Send Signals to Multiple Processes:
kill -9 <PID1> <PID2> <PID3>
These notes provide a comprehensive overview of the kill command, its usage, signals, and related commands.