Despite its name, kill is a tool for sending signals. Ending a process is simply its most common use.
The basic command
$ sleep 500 &
[1] 4120
$ kill 4120
$
[1]+ Terminated sleep 500With no signal named, kill sends SIGTERM (15). This asks the process to stop and lets it clean up first. The "Terminated" message may appear only after you press Enter again.
Choosing a signal
These three lines are equivalent ways to send SIGKILL:
kill -9 4120
kill -KILL 4120
kill -s KILL 4120SIGKILL ends the process without giving it a chance to react. Unsaved work and temporary files may be left behind. Use it only after SIGTERM has had a few seconds and failed.
Even SIGKILL has limits. A process in uninterruptible sleep (state D) may act on it only after the wait ends, and a zombie is already dead.
Permission
You can signal your own processes. Signalling another user's process normally requires root:
$ kill 778
bash: kill: (778) - Operation not permittedThe special signal 0 sends nothing. It only checks that the process exists and that you may signal it:
$ kill -0 4120 && echo "still there"
still thereKilling by name
pgrep and pkill match processes by name. Always preview with pgrep first:
$ pgrep -a sleep
4300 sleep 500
4301 sleep 600
$ pkill sleeppkill treats its argument as a pattern, so a short word may match more than you expect. Add -x to require an exact name, or -u maria to limit it to one user.
killall (from the psmisc package on Linux) signals every process with exactly the given name. On some other Unix systems, such as Solaris, killall is meant to end all active processes, so check where you are before using it.
Job numbers
In an interactive shell, kill %1 signals job number 1, so you do not need its PID.
Try it
Start two sleep 300 & commands. List them with pgrep -a sleep, end one by PID and the other with kill %2 or its job number as shown by jobs. Confirm with jobs. Use the processes and cron lab if you want a disposable machine.
Check your understanding
Ending processes with kill
All 4 sections ยท no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.