The kill
command is used to send signals to processes, typically to terminate them. While the default action is to terminate a process, kill
can send various signals to control process behavior.
-
Syntax:
kill [options] [PID...]
-
Examples:
kill 1234
- Sends the default
SIGTERM
signal to terminate the process with PID1234
.
kill -9 1234
- Sends the
SIGKILL
signal to forcefully terminate the process with PID1234
.
- Sends the default
-
SIGTERM
(15):- The default signal sent by
kill
. It requests the process to terminate gracefully, allowing it to clean up resources.
kill 1234
- Gracefully terminates the process with PID
1234
.
- The default signal sent by
-
SIGKILL
(9):- Forcefully terminates a process immediately without allowing it to clean up. This is a last resort when a process won't respond to
SIGTERM
.
kill -9 1234
- Immediately kills the process with PID
1234
.
- Forcefully terminates a process immediately without allowing it to clean up. This is a last resort when a process won't respond to
-
SIGHUP
(1):- Often used to reload a process's configuration files without terminating it.
kill -HUP 1234
- Sends a hangup signal to the process with PID
1234
, usually causing it to reload its configuration.
-
SIGSTOP
(19):- Pauses a process without terminating it. The process can be resumed later with
SIGCONT
.
kill -STOP 1234
- Stops the process with PID
1234
.
- Pauses a process without terminating it. The process can be resumed later with
-
SIGCONT
(18):- Resumes a paused process that was stopped with
SIGSTOP
.
kill -CONT 1234
- Resumes the process with PID
1234
.
- Resumes a paused process that was stopped with
-
SIGUSR1
andSIGUSR2
:- User-defined signals that can be programmed to perform specific actions.
kill -USR1 1234
- Sends
SIGUSR1
to the process with PID1234
.
-
Finding the PID:
- Use
ps
,top
, orpgrep
to find the process ID (PID) before usingkill
.
ps aux | grep process_name
- Finds the PID for
process_name
.
- Use
-
Terminating Multiple Processes:
- You can send a signal to multiple processes by listing multiple PIDs.
kill 1234 5678 9101
- Sends
SIGTERM
to PIDs1234
,5678
, and9101
.
-
Sending a Signal by Name:
- You can specify signals by their name instead of the number.
kill -HUP 1234
- Sends
SIGHUP
to PID1234
.
-
Forcefully Killing a Process:
- Use
kill -9
as a last resort to terminate an unresponsive process.
kill -9 1234
- Forcefully kills PID
1234
.
- Use
The kill
command is a versatile tool for managing processes in Linux. It allows users to terminate, pause, resume, or send custom signals to processes. While kill
is often associated with terminating processes, it can be used for a variety of process control tasks, making it an essential tool for system administration and process management.