C.W.K.
Stream
Lesson 03 of 10 · published

kill: Sending Signals

~10 min · kill, signals, term

Level 0Window Tourist
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Kill is misnamed

kill doesn't always kill — it sends a signal. Different signals tell the process different things; the default is SIGTERM ("please shut down"). Programs that handle signals can clean up. The nuclear option, SIGKILL, can't be caught.

The signals you'll use

  • kill PID — same as kill -TERM. Default. Polite shutdown.
  • kill -INT PID — SIGINT. Same as Ctrl+C.
  • kill -HUP PID — SIGHUP. Many daemons treat this as "reload your config" without restarting.
  • kill -USR1 PID — application-defined. nginx uses it for log rotation, for example.
  • kill -KILL PID or kill -9 PID — SIGKILL. Force kill, no cleanup. Last resort.
  • kill -STOP PID / -CONT — pause and resume (like Ctrl+Z then fg).

kill by name

pkill firefox
pkill -f 'python myscript.py'        # match full command
killall Slack                          # macOS GUI apps

The escalation ladder

  1. kill PID — politely.
  2. Wait 5–10 seconds. Most well-behaved programs exit by now.
  3. kill -KILL PID — only if SIGTERM was ignored.

Reaching for -9 immediately is a bad habit; the program can't flush buffers, close files, or release locks. Save it for wedged-process emergencies.

Code

Polite then forceful·bash
PID=$(pgrep -f myserver | head -1)
kill $PID                  # SIGTERM
sleep 5
if kill -0 $PID 2>/dev/null; then
  echo 'still alive — escalating'
  kill -9 $PID
fi

External links

Exercise

In one terminal: sleep 999. In another: pgrep -fa 'sleep 999', then kill <PID> and watch it exit cleanly. Run sleep 999 again, this time kill -9 <PID> and see how the original terminal reports it.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.