Process is the application or software that is currently running. Each process is identified by unique ID called process ID. Processes can create other processes called child or sub processes. There are different tools to look at the processes running including child processees. Processes and resources required by the process are managed by operating system kernel
This article will walk you through the ways to view the process tree using different commands such as
- ps
- tree
- pstree
PS command
The command “ps” is the intrinsic part of every Linux distribution. To display all processes using ps command
ps -waxf
To display process tree use the command line option “–tree”
To display all processes running use the option “-A” or “-e”
# ps -A
Filter Processes by TTY
Display process associated with TTY
ps a
Display processes that are not associated with TTY
ps -a
Display Process by User
To display processes owned by the current user
ps x
To display processes running with respect to user
ps u
To filter the processes by the user selected use the option -U or –User
ps -U user #name or id
or,
ps --User user # name or id
List Processes by Parent
To list all subprocess by parent process ID
ps --ppid parent_process_id
Process Tree by PS
To display the ASCII tree of all processes with subprocesses use the option “f” or “–forest”
ps --forest
Tree command
Do you know, proc file system contains process ID as directories and each directory contains the comprehensive information about that corresponding process including executable path. Such that we can use the traditional tree command to display the process tree
tree /proc
To display only the directories
tree -d /proc
PSTree
Even though traditional “ps” command works well. We have the specific utility which does this single job very well, that is “pstree” command. While “ps” command some what spits out the different kind of output at times compared to Linux counter parts. This command pstree is quite helpful to print process tree on Mac
Install the command pstree
sudo apt install psmisc
Install pstree on Mac
brew install pstree
To display the process tree using pstree command
pstree
To include the processes related to process ID (PID) in the output of pstree command
pstree -p pid
Usage of command pstree
Usage: pstree [-f file] [-g n] [-l n] [-u user] [-U] [-s string] [-p pid] [-w] [pid ...] -f file read input from <file> (- is stdin) instead of running "ps -axwwo user,pid,ppid,pgid,command" -g n use graphics chars for tree. n=1: IBM-850, n=2: VT100, n=3: UTF-8 -l n print tree to n level deep -u user show only branches containing processes of <user> -U don't show branches containing only root processes -s string show only branches containing process with <string> in commandline -p pid show only branches containing process <pid> -w wide output, not truncated to window width pid ... process ids to start from, default is 1 (probably init)
Other command line tools are available on Linux presenting GUI using ncurses. Which are used for monitoring and process management such as
- atop
- vtop
- htop
- btop++
- nmon
- Glances