Linux kill command options


The kill command in Linux is used to send signals to processes. While the most common use of kill is to terminate processes, there are various options and signals that can be sent, each with different effects. Below are some commonly used options with examples of how to use them.

1. kill -l (List all available signals)

The -l option is used to list all available signals that can be sent to a process. This is helpful to know the signal number or name when using kill.

Example:

kill -l

Sample Output:

1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 ...
  • This command lists all the available signals that can be used with the kill command.

2. kill -s <signal_name> (Send a specific signal)

The -s option allows you to specify the signal you want to send by name instead of by number. You can use any of the signal names listed with kill -l.

Example:

kill -s SIGTERM 1234

Sample Output:

[no output]
  • This sends the SIGTERM (terminate) signal to the process with PID 1234.

3. kill -9 (Send SIGKILL signal)

The -9 option sends the SIGKILL signal, which immediately terminates a process. This signal cannot be caught or ignored by the process.

Example:

kill -9 1234

Sample Output:

[no output]
  • This forcefully kills the process with PID 1234. It is used when a process doesn't respond to a normal termination signal (SIGTERM).

4. kill -15 (Send SIGTERM signal)

The -15 option sends the SIGTERM (terminate) signal, which is the default signal sent by the kill command. It gracefully asks the process to terminate. This is the default behavior of kill, so you can use kill without specifying a signal to send SIGTERM.

Example:

kill -15 1234

Sample Output:

[no output]
  • This sends a request to the process with PID 1234 to terminate. This signal can be caught by the process, allowing it to clean up resources before exiting.

5. kill -STOP (Pause a process)

The -STOP option sends the SIGSTOP signal, which pauses (stops) a process. The process can later be resumed using the SIGCONT signal.

Example:

kill -STOP 1234

Sample Output:

[no output]
  • This pauses the process with PID 1234. The process will not consume CPU until it is resumed.

6. kill -CONT (Resume a paused process)

The -CONT option sends the SIGCONT signal, which resumes a process that was previously paused with SIGSTOP or SIGTSTP.

Example:

kill -CONT 1234

Sample Output:

[no output]
  • This resumes the paused process with PID 1234.

7. kill -u <username> (Send signal to all processes owned by a specific user)

The -u option allows you to send a signal to all processes owned by a specific user.

Example:

kill -9 -u john

Sample Output:

[no output]
  • This sends the SIGKILL signal to all processes owned by the user john. It is often used to forcefully terminate all processes belonging to a user.

8. kill -p <pid> (Send signal to a specific process by PID)

You can send a signal to a specific process by providing its PID directly without any additional options.

Example:

kill -p 1234

Sample Output:

kill: cannot send signal to PID 1234: No such process
  • This shows an error if the process with PID 1234 does not exist. Normally, kill with a PID would work just like using kill <pid> without options.

9. kill -t <signal> (Send a specific signal to a process)

The -t option can be used to specify the type of signal you want to send, similar to the -s option. The difference is that this syntax is more concise.

Example:

kill -t SIGTERM 1234

Sample Output:

[no output]
  • This sends the SIGTERM signal to the process with PID 1234.

10. killall Command (Kill Processes by Name)

The killall command is a variant of kill that sends signals to processes by name, rather than PID. This is useful when you want to kill all instances of a particular program.

Example:

killall firefox

Sample Output:

[no output]
  • This command will terminate all processes named firefox running on the system.

Conclusion:

The kill command in Linux is used to send signals to processes. By using various options like -l, -9, -s, -STOP, and -u, you can control the behavior of processes. Commonly used signals include SIGTERM (terminate), SIGKILL (forcefully terminate), SIGSTOP (pause), and SIGCONT (resume). For mass termination by name, you can use the killall command. Always use SIGKILL as a last resort, as it forcefully terminates a process without giving it a chance to clean up.