ps is a snapshot
The ps program reads the process table once and prints what it found. Its options come in two families. The BSD style has no dash, as in ps aux. The POSIX style uses a dash, as in ps -ef. Both styles work in the lab because the installed ps is from procps-ng, which accepts both. The two families print slightly different default columns, so read the header before you trust a column.
A common starting point is to list every process with a few columns. The head command keeps the output short so that the first lines are easy to read.
ps -eo pid,ppid,stat,comm | head -n 6The -e option selects every process, and -o picks the columns. The first line of output is the header, followed by the processes in PID order. PID 1 comes first, and later lines show the children it created.
Filtering the list
Scanning a long list by eye is slow. The -C option selects processes by command name, and -p selects by PID. The pgrep program from the same package prints only the PIDs that match a name, which is useful in scripts. The args column shows the full command line, which separates two processes that share a name, such as two instances of a shell script.
ps -C sleep -o pid,args
pgrep -x bashIf no sleep process is running, the first command prints only its header. The pgrep -x form matches the whole command name exactly, so it does not match a longer name that merely starts with the same letters.
The difference between these tools is mostly about time. Each one gives you a view of one moment, and the only way to see a trend is to repeat the view. A process whose CPU time keeps growing between two snapshots is busy, while one whose CPU time stays flat is idle even though it is running. When you need that comparison, save two snapshots to files and compare them with diff, or let top refresh for a few seconds. A process that appears in one snapshot and not in the next is not necessarily a problem, because short-lived commands come and go all the time.
Showing the tree
The pstree program, from the psmisc package, draws the parent and child relationships as indented text. With -p it adds the PID to each name. Run it on your own shell to see how far up the tree it goes.
pstree -p $$The output starts at the top process of the branch that contains your shell and descends to the shell itself. If you run this from a child of your shell, such as a command substitution, the child is shown indented under the shell. This view is the fastest way to answer the question which program started this one.
top for changing load
The top program refreshes the list every few seconds, and by default it sorts by CPU use. Interactive use needs a full terminal. In the lab you can ask for one batch snapshot with -b and -n 1, and then pipe it to head. The first lines show the load averages, the task counts by state, and the memory summary, followed by the top processes.
top -b -n 1 | head -n 12The task line counts processes by state, using the same letters as the STAT column. The summary is useful when you want to know whether the machine is busy, and the process lines below it tell you which process is busy.
In an interactive top, press k to kill a process by PID, and q to quit. Be careful with k, because it sends SIGTERM to the process you name, which is a real signal to a real program.
/proc is the source
Every tool above reads the /proc file system. It is not a real disk. The kernel generates each file when you read it. Each process has a directory named after its PID, and the files inside describe it. The cmdline file holds the arguments separated by NUL bytes, and the cwd link points at the current directory. The /proc/self name always refers to the process that reads it.
tr "\0" " " < /proc/self/cmdline; echoThe command prints the arguments of the tr process itself, which shows that /proc/self follows the reader. The NUL separators become spaces so the output is readable. Reading /proc directly is useful when no tool is installed, which is the case on some minimal systems, and it is also how you check a value that a tool does not show.
Hands-on: pick the right view
Work through this short sequence. First, count how many processes exist. Then list the processes whose state begins with S, and finally draw the tree from your shell. Each command answers one question, and together they show how the three tools complement each other.
ps -e --no-headers | wc -l
ps -eo stat,comm --no-headers | grep -c "^S"
pstree -p $$The counts change from run to run, so the exact numbers are not the point. Notice that the tree output and the process count come from the same table, and that a state filter uses the letters you learned in the previous unit. Use ps for a snapshot, top to watch change, and /proc when you need one raw value.