Linux kill command signals
The kill
command in Linux is used to send signals to processes to control their behavior, such as terminating them, pausing them, or handling other system events. Signals are used to communicate with processes, and each signal has a specific purpose. Below is an explanation of common signals you can send using the kill
command, along with examples and expected outputs.
Common Kill Command Signals in Linux
1. SIGTERM (15) - Terminate Process (Gracefully)
- Signal Number:
15
- Default Signal: This is the default signal sent by the
kill
command if no signal is specified. - Purpose: Gracefully asks a process to terminate. The process can catch this signal and clean up resources before exiting.
Example:
or
Sample Output:
- The process with PID
1234
will be asked to terminate. It may clean up resources and exit gracefully.
2. SIGKILL (9) - Forcefully Terminate Process
- Signal Number:
9
- Purpose: Immediately terminates the process. This cannot be caught, blocked, or ignored by the process, making it a more forceful way to terminate processes.
Example:
Sample Output:
- The process with PID
1234
will be immediately terminated without any cleanup.
3. SIGSTOP (19) - Pause Process
- Signal Number:
19
- Purpose: Pauses a running process. The process will be stopped and will not consume any CPU resources until it is resumed with
SIGCONT
.
Example:
Sample Output:
- The process with PID
1234
is paused and will not continue until resumed.
4. SIGCONT (18) - Resume Process
- Signal Number:
18
- Purpose: Resumes a paused process that was previously stopped using
SIGSTOP
orSIGTSTP
.
Example:
Sample Output:
- The process with PID
1234
will resume execution after being paused.
5. SIGINT (2) - Interrupt Process
- Signal Number:
2
- Purpose: Interrupts a running process, similar to pressing
Ctrl + C
in the terminal. This is typically used to stop processes interactively.
Example:
Sample Output:
- The process with PID
1234
will be interrupted and will usually terminate unless it catches this signal.
6. SIGHUP (1) - Hang Up Process
- Signal Number:
1
- Purpose: Often used to instruct a process to reload its configuration files or to clean up resources before terminating. It is also used by some processes to terminate when the controlling terminal is closed.
Example:
Sample Output:
- The process with PID
1234
will typically reload its configuration or terminate, depending on how it handles this signal.
7. SIGQUIT (3) - Quit Process and Generate Core Dump
- Signal Number:
3
- Purpose: Causes the process to quit and create a core dump. This can be useful for debugging purposes as the core dump captures the process state at the time of termination.
Example:
Sample Output:
- The process with PID
1234
will quit and generate a core dump file that can be analyzed for debugging.
8. SIGPIPE (13) - Broken Pipe (Write to a Closed Pipe)
- Signal Number:
13
- Purpose: This signal is sent when a process attempts to write to a pipe that has no readers (i.e., the receiving process is terminated). This is typically used to stop processes that are involved in communication through pipes.
Example:
Sample Output:
- The process with PID
1234
will receive theSIGPIPE
signal and may terminate if it does not handle this signal properly.
9. SIGUSR1 (10) and SIGUSR2 (12) - User-Defined Signals
- Signal Numbers:
10
and12
- Purpose: These signals are intended for user-defined purposes. They can be used by the application to trigger custom behavior, such as reloading configuration or performing internal actions.
Example:
Sample Output:
- The process with PID
1234
will handle the signal according to the application-defined behavior forSIGUSR1
orSIGUSR2
.
10. SIGALRM (14) - Alarm Clock Signal
- Signal Number:
14
- Purpose: Typically used by applications to indicate that a timer has expired. It is often used for timeout-based behavior in processes.
Example:
Sample Output:
- The process with PID
1234
will receive theSIGALRM
signal, and it can act on this signal (e.g., terminate or handle timeouts).
Sending Signals to Multiple Processes
You can send a signal to multiple processes by listing their PIDs in a single command.
Example:
Sample Output:
- This sends the
SIGKILL
signal to the processes with PIDs1234
,5678
, and91011
.
Summary of Common Kill Command Signals
Signal | Signal Number | Description | Default Action |
---|---|---|---|
SIGTERM | 15 | Gracefully terminate the process | Terminate |
SIGKILL | 9 | Forcefully terminate the process | Immediately terminate |
SIGSTOP | 19 | Pause (stop) the process | Pause the process |
SIGCONT | 18 | Resume the process | Resume execution |
SIGINT | 2 | Interrupt the process (like Ctrl+C) | Interrupt (terminate) |
SIGHUP | 1 | Hang up the process (reload config) | Terminate or reload configuration |
SIGQUIT | 3 | Quit and create a core dump | Terminate and generate a core dump |
SIGPIPE | 13 | Broken pipe error | Terminate the process if it doesn't handle this |
SIGUSR1 | 10 | User-defined signal 1 | Custom behavior defined by application |
SIGUSR2 | 12 | User-defined signal 2 | Custom behavior defined by application |
SIGALRM | 14 | Alarm signal (timeout) | Handle timeout behavior |
Conclusion
The kill
command with different signals provides a flexible way to control processes in Linux. Signals like SIGTERM
and SIGKILL
are commonly used for termination, while others like SIGSTOP
and SIGCONT
are used for pausing and resuming processes. The SIGUSR1
and SIGUSR2
signals allow applications to define custom behaviors. By sending the appropriate signals, you can manage process behavior effectively.