Kahibaro
Discord Login Register

Killing processes

Understanding When and Why to Kill Processes

Killing a process means sending it a signal that tells it to stop doing what it’s doing—often to end it. You normally do this when:

In Linux, you don’t “kill” by name alone; you send signals to processes identified by a PID (process ID) or sometimes by name. How to see running processes is covered in the “Viewing processes” chapter; here we’ll focus on stopping them.

Signals Basics (What `kill` Actually Does)

The kill command doesn’t always “brutally” kill something. It sends a signal. The most common:

You can refer to signals by name (e.g. -SIGTERM, -TERM) or number (e.g. -15).

To see available signals on your system:

kill -l

The `kill` Command (By PID)

Basic usage

General syntax:

kill [options] PID...

Examples:

  kill 1234
  kill -TERM 1234
  # or
  kill -15 1234
  kill -KILL 1234
  # or
  kill -9 1234

You can kill multiple PIDs at once:

kill 1234 2345 3456

Finding the PID to kill

This is covered more deeply in “Viewing processes”, but quick examples:

Using ps:

ps aux | grep firefox

You’d then pick the correct PID from the output and:

kill 9876

Using pgrep (matches process names):

pgrep firefox
kill 9876

Or combine:

pkill -TERM firefox

(more on pkill below)

Killing Processes by Name: `pkill` and `killall`

Sometimes you don’t want to manually look up a PID; you’d rather kill “all firefox processes”.

`killall`

killall kills processes by exact name:

killall firefox

This sends SIGTERM to all processes named firefox. To force kill:

killall -KILL firefox
# or
killall -9 firefox

Common useful options (varies slightly by distribution):

  killall -u alice firefox
  killall -i firefox

Warning: On some Unix-like systems, killall has different behavior (e.g., kill all processes). On typical Linux distributions it’s “kill by name”, but always check man killall on your system.

`pkill`

pkill is similar but more flexible; it can match patterns:

  pkill firefox
  pkill -TERM firefox
  pkill -KILL firefox
  pkill -u alice firefox
  pkill -f "python my_script.py"

As with killall, use with care: you might kill more than you intended if your pattern is too broad.

Stopping Foreground Programs (Keyboard Shortcuts)

When a program runs in the terminal foreground:

Use Ctrl+C as your first “stop” action for something you just started in the terminal.

Choosing the Right Signal

General guideline:

  1. Ask nicely first: SIGTERM (or Ctrl+C if foreground).
    • Example:
     kill 1234
     # or
     killall firefox
  1. If it doesn’t respond after a short wait, try again and check if the process is stuck (e.g., using ps, top, or htop).
  2. As a last resort, use SIGKILL:
   kill -9 1234
   pkill -9 firefox

Why not jump to SIGKILL?

Permission Rules When Killing Processes

You can only send signals to:

If you see:

bash: kill: (1234) - Operation not permitted

That process is likely owned by another user or by root. You can:

sudo kill 1234

Use extra caution when killing processes as root; you can easily stop critical system services.

Killing Background Jobs from Your Shell

For commands started in the current shell:

  jobs

Output example:

  [1]+  Running    sleep 300 &
  kill %1

You can convert a job number to a PID (if you want):

ps
# or
ps -o pid,cmd

But kill %jobnumber is usually easier for quick, recent background jobs.

Being Safe and Selective

Before killing anything, especially as root:

  1. Identify the process properly.
    • Use ps, ps aux | grep ..., pgrep, or tools like top/htop.
  2. Check the command line to be sure it’s the right thing.
  3. Avoid killing obvious system-critical processes (e.g. systemd, init, sshd, database servers) unless you know exactly what you’re doing.

For practice, you can safely experiment with harmless commands like:

sleep 1000 &
ps
kill <that PID>

This lets you see how kill behaves without risking your system.

Summary of Common Commands

  kill 1234
  kill -9 1234
  killall firefox
  pkill firefox
  killall -KILL firefox
  pkill -9 firefox
  kill -l

Mastering these will let you recover from stuck programs and keep control over your system from the command line.

Views: 22

Comments

Please login to add a comment.

Don't have an account? Register now!