brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ps --forest draws the process tree, so you see who started what

Ps
~/tips/tip-ps-forest-process-tree

ps --forest -o pid,ppid,cmd -s <session-id>

Prints processes with ASCII art lines showing which process is the parent of which.

A flat ps list tells me a process exists. It does not tell me what started it, and that is usually my actual question. ps can draw the tree itself.

ps --forest -o pid,ppid,cmd -s <session-id>

The manual calls --forest an ASCII art process tree. -o picks the columns, and -s limits the output to one session. With a shell that started bash -c 'sleep 61 & sleep 62 & wait', procps-ng 4.0.4 printed this (long command lines trimmed from the other rows):

  PID  PPID CMD
 2031  2030  \_ bash -c sleep 61 & sleep 62 & wait
 2033  2031  |   \_ sleep 61
 2034  2031  |   \_ sleep 62

The PPID column of the two sleep rows is the PID of the bash -c row above them, and the indentation makes the same point without reading numbers. For everything on the machine, use ps -e --forest -o pid,ppid,cmd. -e selects all processes, and the output starts with the kernel threads under kthreadd, so it gets long. Pipe it into less -S or pick a start with -C, which selects processes by executable name.

The manual also lists -H (“show process hierarchy”) and the BSD-style f for the same thing, so ps -eHo pid,ppid,cmd and ps axf are alternatives if one spelling is not accepted where you are.

One caveat: cmd prints the whole command line. In a session started by another tool the first row was several hundred characters long, so pipe the result through cut -c1-120 when the lines wrap.

Further reading: ps(1) manual

More Ps tips ps aux --sort=-%mem | head -n 6 All Ps tips (2) All topics

Link