0
0
Linux-cliHow-ToBeginner · 3 min read

How to Use the kill Command in Linux: Syntax and Examples

Use the kill command followed by a process ID (PID) to send signals that stop or control processes in Linux. For example, kill 1234 sends the default TERM signal to stop process 1234 safely.
📐

Syntax

The basic syntax of the kill command is:

  • kill [options] <PID> - Sends a signal to the process with the given PID.
  • -s <signal> or -<signal> - Specify which signal to send (e.g., TERM, KILL).
  • -l - List all available signals.

Common signals include TERM (terminate gracefully) and KILL (force stop immediately).

bash
kill [options] <PID>

# Examples:
kill 1234
kill -9 1234
kill -s KILL 1234
kill -l
💻

Example

This example shows how to find a process ID and stop it using kill. It demonstrates sending the default TERM signal and the forceful KILL signal.

bash
# Start a background process (sleep for 100 seconds)
sleep 100 &

# Get the PID of the sleep process
PID=$!

echo "Process ID is $PID"

# Send TERM signal to stop it gracefully
kill $PID

# Check if process is still running
ps -p $PID

# If still running, force kill it
kill -9 $PID

# Check again
ps -p $PID
Output
Process ID is 12345 PID TTY TIME CMD # (No output after kill -9 means process stopped)
⚠️

Common Pitfalls

Common mistakes when using kill include:

  • Using the wrong PID or no PID, which results in errors or killing the wrong process.
  • Not using sudo when trying to kill processes owned by other users.
  • Using kill -9 (SIGKILL) too often, which stops processes immediately without cleanup.
  • Confusing signal names and numbers.

Always try kill without -9 first to allow graceful shutdown.

bash
## Wrong way: Killing without PID
kill

## Right way: Specify PID
kill 1234

## Wrong way: Using kill -9 immediately
kill -9 1234

## Better way: Try TERM first
kill 1234
sleep 2
kill -9 1234
Output
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec] # No output for correct kill commands
📊

Quick Reference

CommandDescription
kill Send TERM signal to stop process gracefully
kill -9 Send KILL signal to force stop process immediately
kill -lList all available signals
kill -s SIGINT Send interrupt signal to process
kill -s SIGSTOP Pause the process
kill -s SIGCONT Resume a paused process

Key Takeaways

Use kill with the process ID to stop or control processes in Linux.
Try the default TERM signal before using the forceful KILL signal (-9).
Always verify the PID to avoid stopping the wrong process.
Use kill -l to see all available signals you can send.
Use sudo if you need permission to kill processes owned by other users.