kill – Send a signal to a process

The kill command is used to send a signal to a process in Linux. It allows you to terminate a process or modify its behavior by sending a specific signal to it.

Overview

The syntax for using the kill command is as follows:

kill [signal or option] PID

where signal is the signal you want to send to the process, and PID is the process ID of the process you want to send the signal to.

For example, to terminate a process with a specific PID, you can use the following command:

kill 1234

This will send the default SIGTERM signal to the process with PID 1234, asking it to terminate gracefully. If the process does not terminate within a certain amount of time, you can use the SIGKILL signal to forcefully terminate it:

kill -9 1234

This will send the SIGKILL signal to the process with PID 1234, which will immediately terminate the process.

You can also send signals to multiple processes at once by specifying their PIDs separated by spaces:

kill 1234 5678 9012

Common Signals

Here are some of the most commonly used signals with the kill command:

Signal Name Description
SIGTERM Terminate Ask the process to terminate gracefully
SIGKILL Kill Forcefully terminate the process
SIGSTOP Stop Stop the process (can be resumed later with SIGCONT)
SIGCONT Continue Resume a stopped process

Options

Here are some of the available options for the kill command:

Option Description
-l List all available signals
-s Specify a signal by name or number (e.g. -s SIGTERM or -s 15)
-p Don’t send the signal to the process group, only to the specified PID
-u Send the signal to all processes owned by a specific user

Troubleshooting Tips

If you’re having trouble terminating a process with the kill command, here are some tips you can try:

  • Make sure you have the correct PID for the process you want to terminate. You can use the ps command to list all running processes and their PIDs.
  • If the process is not terminating with the SIGTERM signal, try using the SIGKILL signal instead.
  • If you’re getting a “Permission denied” error, make sure you’re running the kill command as a user with sufficient privileges (e.g. root).

Notes

  • The kill command can be a powerful tool, but use it with caution. Terminating a process abruptly can cause data loss or other issues.
  • If you’re unsure about which signal to send to a process, the SIGTERM signal is usually a good starting point. It asks the process to terminate gracefully, giving it a chance to clean up any resources it’s using before exiting.