Signals
You stop a process by sending it a signal, a small message from the kernel. Despite its name, kill is the command that sends signals. By default it sends TERM, which politely asks the program to finish.
This machine has a report-builder that got stuck. Find its PIDs, then stop them. $( ) puts the PIDs from pgrep into the command, so you don't have to type them:
pgrep -a report-builder
kill $(pgrep report-builder)
pgrep -a report-builderIn the terminal
student@vulntech:~$ pgrep -a report-builder 1604 /bin/bash /usr/local/bin/report-builder 1611 /bin/bash /usr/local/bin/report-builder student@vulntech:~$ kill $(pgrep report-builder) student@vulntech:~$ pgrep -a report-builder
The last pgrep printed nothing: they're gone. pkill does both steps in one, and killall is similar but matches the exact program name:
pkill report-builder
pgrep -c report-builderIn the terminal
student@vulntech:~$ pkill report-builder student@vulntech:~$ pgrep -c report-builder 0
The signals you'll use
| Signal | Number | What it means |
|---|---|---|
TERM | 15 | Please finish (the default). The program can clean up first. |
INT | 2 | Interrupt: what Ctrl+C sends |
HUP | 1 | Hang up: the terminal closed. Many services reload their settings when they get it. |
KILL | 9 | Stop now. The program can't catch or ignore it. |
STOP / CONT | 19 / 18 | Pause and continue |
Choose a signal by name or number: kill -HUP 1234, kill -9 1234. kill -l lists them all.
When TERM isn't enough
A program can ignore TERM, and a badly stuck one may never get round to finishing. stubborn ignores polite signals, so it survives pkill. Then, and only then, use KILL:
pkill stubborn
pgrep -a stubborn
pkill -KILL stubborn
pgrep -a stubbornIn the terminal
student@vulntech:~$ pkill stubborn student@vulntech:~$ pgrep -a stubborn 1618 /bin/bash /usr/local/bin/stubborn student@vulntech:~$ pkill -KILL stubborn student@vulntech:~$ pgrep -a stubborn
Common mistake: Don't reach for
kill -9first.KILLgives the program no chance to save its work, delete its temporary files or tell anyone it's leaving. TryTERM, wait a few seconds, and only then use-9.
Other users' processes
You may only signal your own processes. The web server belongs to www, so stopping it takes sudo:
pkill webserver
sudo pkill webserver
pgrep -c webserverIn the terminal
student@vulntech:~$ pkill webserver pkill: killing pid 1625 failed: Operation not permitted student@vulntech:~$ sudo pkill webserver [sudo] password for student: student@vulntech:~$ pgrep -c webserver 0
Remember:
kill PID,pkill nameandkillall namesendTERM.-9(KILL) is the last resort. Other users' processes needsudo.