Linux commands
Browse topics

Inspecting processes with ps

Version
unversioned
Review
Draft

Summary#

ps prints a snapshot of processes. Without selection options it shows only a narrow set, typically the processes of your terminal.

Syntax#

sh
ps -ef
ps aux
ps -o pid,ppid,stat,args -p "$pid"

ps -ef and ps aux both list every process, with different columns. -o chooses columns and -p selects process IDs.

Example#

sh
(
  set -eu
  child=
  trap 'if [ -n "$child" ]; then
    kill "$child" 2>/dev/null || true
    wait "$child" 2>/dev/null || true
  fi' EXIT
  sleep 30 &
  child=$!
  until [ "$(ps -o comm= -p "$child")" = sleep ]; do sleep 0.1; done

  ps -o comm= -p "$child"
  ps -o args= -p "$child"
  found=$(ps -o pid= -p "$child" | tr -d ' ')
  test "$found" = "$child"
  echo 'ps found the PID stored by the shell'
)

Expected output#

Text
sleep
sleep 30
ps found the PID stored by the shell

Common mistakes#

Plain ps lists only your own processes on the current terminal, so a service started elsewhere seems to be missing; select it with -e or -p. A search such as ps aux | grep name can also match the grep command itself. Write ps aux without a dash. With a dash, procps can interpret x as a user name.

Version applicability#

This entry describes ps from procps-ng. BusyBox, BSD and macOS ps differ in options and column names.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0