Skip to content

Linux Foundations / Manage a Linux host ยท Step 28 of 50

Lesson: Ending processes with killSwitch
โ† Linux Foundations

System administration

Ending processes with kill

Send signals safely with kill, pkill and killall.

Despite its name, kill is a tool for sending signals. Ending a process is simply its most common use.

The basic command

bash
$ sleep 500 &
[1] 4120
$ kill 4120
$
[1]+  Terminated              sleep 500

With 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:

bash
kill -9 4120
kill -KILL 4120
kill -s KILL 4120

SIGKILL 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:

bash
$ kill 778
bash: kill: (778) - Operation not permitted

The special signal 0 sends nothing. It only checks that the process exists and that you may signal it:

bash
$ kill -0 4120 && echo "still there"
still there

Killing by name

pgrep and pkill match processes by name. Always preview with pgrep first:

bash
$ pgrep -a sleep
4300 sleep 500
4301 sleep 600
$ pkill sleep

pkill 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.