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:

kill 1234

or

kill -15 1234

Sample Output:

[no 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:

kill -9 1234

Sample Output:

[no 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:

kill -19 1234

Sample Output:

[no 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 or SIGTSTP.

Example:

kill -18 1234

Sample Output:

[no 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:

kill -2 1234

Sample Output:

[no 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:

kill -1 1234

Sample Output:

[no 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:

kill -3 1234

Sample Output:

[no 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:

kill -13 1234

Sample Output:

[no output]
  • The process with PID 1234 will receive the SIGPIPE signal and may terminate if it does not handle this signal properly.

9. SIGUSR1 (10) and SIGUSR2 (12) - User-Defined Signals

  • Signal Numbers: 10 and 12
  • 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:

kill -10 1234 # Sends SIGUSR1 kill -12 1234 # Sends SIGUSR2

Sample Output:

[no output]
  • The process with PID 1234 will handle the signal according to the application-defined behavior for SIGUSR1 or SIGUSR2.

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:

kill -14 1234

Sample Output:

[no output]
  • The process with PID 1234 will receive the SIGALRM 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:

kill -9 1234 5678 91011

Sample Output:

[no output]
  • This sends the SIGKILL signal to the processes with PIDs 1234, 5678, and 91011.

Summary of Common Kill Command Signals

SignalSignal NumberDescriptionDefault Action
SIGTERM15Gracefully terminate the processTerminate
SIGKILL9Forcefully terminate the processImmediately terminate
SIGSTOP19Pause (stop) the processPause the process
SIGCONT18Resume the processResume execution
SIGINT2Interrupt the process (like Ctrl+C)Interrupt (terminate)
SIGHUP1Hang up the process (reload config)Terminate or reload configuration
SIGQUIT3Quit and create a core dumpTerminate and generate a core dump
SIGPIPE13Broken pipe errorTerminate the process if it doesn't handle this
SIGUSR110User-defined signal 1Custom behavior defined by application
SIGUSR212User-defined signal 2Custom behavior defined by application
SIGALRM14Alarm 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.