Linux kill command


The kill command in Linux is used to send signals to processes. By default, it sends the SIGTERM (terminate) signal to a process, but you can specify other signals (such as SIGKILL) to control the behavior of processes. The kill command is primarily used to terminate a process, but it can also be used to send other types of signals to processes.

Basic Syntax:

kill [options] <pid>
  • <pid>: The process ID (PID) of the process you want to send a signal to.

Default Behavior:

By default, kill sends the SIGTERM signal, which politely asks the process to terminate.

kill <pid>

Commonly Used Signals with kill:

  • SIGTERM (15): Terminate the process (default signal sent by kill).
  • SIGKILL (9): Forcefully terminate the process. This cannot be ignored by the process.
  • SIGINT (2): Interrupt a process, similar to pressing Ctrl + C in the terminal.
  • SIGSTOP (19): Stop (pause) a process. This can be resumed later using SIGCONT.
  • SIGCONT (18): Resume a paused process.
  • SIGHUP (1): Hang up the process, often used to reload a process (such as a daemon).

Example Usage:

  1. Terminate a Process (SIGTERM): To send the default SIGTERM signal to a process with PID 1234:

    kill 1234

    Sample Output (in the terminal):

    [no output]
    • The process with PID 1234 will be asked to terminate gracefully.
  2. Forcefully Kill a Process (SIGKILL): If the process does not terminate after receiving SIGTERM, you can forcefully kill it using SIGKILL (signal 9). This will immediately stop the process, regardless of its state.

    kill -9 1234

    Sample Output (in the terminal):

    [no output]
    • The process with PID 1234 is immediately killed without any cleanup.
  3. Interrupt a Process (SIGINT): To simulate pressing Ctrl + C in the terminal to interrupt a running process, use the SIGINT signal.

    kill -2 1234

    Sample Output (in the terminal):

    [no output]
    • The process with PID 1234 will be interrupted, and the process will terminate if it handles SIGINT.
  4. Pause a Process (SIGSTOP): To stop a running process, you can send the SIGSTOP signal. This pauses the process and it can be resumed later.

    kill -19 1234

    Sample Output (in the terminal):

    [no output]
    • The process with PID 1234 is paused and will not consume CPU until resumed.
  5. Resume a Process (SIGCONT): After pausing a process with SIGSTOP, you can resume it with SIGCONT.

    kill -18 1234

    Sample Output (in the terminal):

    [no output]
    • The process with PID 1234 resumes execution.

Kill Multiple Processes:

You can send a signal to multiple processes at once by specifying multiple PIDs.

kill -9 1234 5678 91011

Sample Output (in the terminal):

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

Using killall:

The killall command can be used to kill processes by name rather than by PID. This is useful if you want to terminate all processes with the same name.

killall <process_name>

For example, to kill all processes with the name firefox:

killall firefox

Sample Output (in the terminal):

[no output]
  • All processes with the name firefox are terminated.

Displaying Available Signals:

You can list all available signals that can be used with kill by using the -l option.

kill -l

Sample Output:

1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS
  • This will display a list of signals that can be used with kill.

Conclusion:

The kill command is a powerful tool to manage processes in Linux by sending signals that can terminate, pause, or interrupt processes. You can use it to gracefully terminate processes with the default SIGTERM signal, or forcefully terminate them with SIGKILL. By combining it with other signals like SIGSTOP and SIGCONT, you can control processes effectively for various use cases, including pausing and resuming processes.