Read

Module 6 · Processes and jobs

killall, pidof and finding a process by name

Turn a program name into PIDs with pidof, signal every copy of a program at once with killall, and fall back on ps and grep when the name alone is not precise enough.

What you will learn

  • Get the PIDs of a program by name with `pidof`, and use `-s`, `-o` and its exit status.
  • Send any signal to every process with a given name using `killall`.
  • Recognise when name matching is too coarse and target one instance with `ps -o pid,args | grep`.

From names to numbers

kill speaks in PIDs, but people think in names: *stop the backup*, *restart the web server*, *how many shells are open?*. Copying numbers out of ps works, yet it is slow and error-prone. Two small tools do the lookup for you. pidof NAME prints the PIDs of every process running the program NAME. killall NAME skips the printing and sends a signal straight to all of them. On full distributions you will also meet pgrep and pkill, which match patterns instead of exact names; this BusyBox does not include them, so here pidof, killall and the ps pipelines from lesson 52 are the whole toolbox.

~% sleep 1000 &
[1] 883
~% sleep 2000 &
[2] 884
~% pidof sleep
884 883
~% pidof -s sleep
884
~% pidof sh
881 859 855
~% pidof nosuch; echo $?
1

pidof prints all the matches on one line, separated by spaces, which is exactly the shape kill wants: kill $(pidof sleep) sends TERM to both sleeps. With -s it prints a single PID, handy when you need to plug one number into a path such as /proc/$(pidof -s sleep)/status. -o PID omits a PID from the answer, and the special form -o %PPID omits the shell that ran pidof, which is how a script asks *is another copy of me already running?*. The exit status is 0 when something matched and 1 when nothing did, so if pidof sleep >/dev/null; then … is a clean *is it running?* test.

What counts as the name

Both tools compare against the program name, the same short name ps -o comm shows, not the full command line. pidof 'sleep 1000' finds nothing, and pidof sleep cannot tell your two sleeps apart. Scripts are a classic surprise: a script started as sh backup.sh is a process named sh, so killall sh would hit every shell on the machine, including your own. When the name is not precise enough, go back to the command line: ps -o pid,args | grep '[s]leep 2000' finds exactly one instance, and awk '{print $1}' extracts its PID for kill.

killall: signal by name

~% killall sleep
[2]+  Terminated                 sleep 2000
[1]+  Terminated                 sleep 1000
~% killall sleep
killall: sleep: no process killed
~% killall -q sleep; echo $?
1
~% yes > /dev/null &
~% killall -STOP yes
~% killall -CONT yes
~% killall -9 yes

killall takes signals exactly like kill (lesson 55): nothing means TERM, and -9, -KILL, -HUP, -STOP or -CONT choose another. -l lists the signal names and -q keeps it quiet when nothing matched, useful in scripts that clean up *if* something is running. Its exit status is 0 if at least one process received the signal. Treat it with the same respect as kill -9: one command can reach dozens of processes, so run pidof NAME first and look at what you are about to hit.

You want…Command
All PIDs of a programpidof NAME
Just one PIDpidof -s NAME
Ask politely, all of themkillall NAME
A specific signal, all of themkillall -HUP NAME
One instance among manyps -o pid,args | grep '[n]ame args'
Is it running? (scripts)pidof NAME >/dev/null

Commands in this lesson

CommandWhat it does
pidof NAMEPIDs of every process running program NAME.
pidof -s NAMEOnly one PID.
pidof -o %PPID NAMEOmit the calling shell (scripts looking for other copies of themselves).
kill $(pidof NAME)Send TERM to all of them via command substitution.
killall NAMESend TERM to every process named NAME.
killall -9 NAME / killall -STOP NAMEAny other signal, by number or name.
killall -q NAMEStay quiet if nothing matched.

Quiz

  1. `pidof nginx` prints nothing. What is its exit status?

    • 0
    • 1
    • 127
  2. Which signal does `killall sleep` send?

    • KILL
    • TERM
    • HUP
  3. `sleep 1000` and `sleep 2000` are running. You want to end only `sleep 2000`. What works?

    • `killall 'sleep 2000'`
    • `kill` with the PID found by `ps -o pid,args | grep '[s]leep 2000'`
    • `pidof -s sleep`, then kill it
  4. Why is `killall sh` a bad way to stop a script started as `sh backup.sh`?

    • killall cannot signal scripts
    • It also hits every other shell, including your own
    • Scripts ignore TERM
  5. Which command sends HUP to every process named `httpd`?

    • `killall -HUP httpd`
    • `pidof -HUP httpd`
    • `kill -HUP httpd`

Practice

  1. Two `sleep` processes are running. Use `pidof` to write all their PIDs into `/root/lab/l56/pids.txt`.

  2. Three `sleep` processes are running. End all of them with a single command that uses the program name, not PIDs.

  3. Two `sleep` processes are running. Freeze (stop) all of them at once, by name, so that `ps` shows them in state `T`.

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.