Linux/Unix: pstree Command Examples: See A Tree Of Processes
I am a new Linux user. How do I display the process on the Linux based server or desktop/laptop in easy to read tree format using bash shell prompt?
Linux and Unix are multitasking operating systems i.e. a system that can run multiple tasks (process) during the same period of time. A process is nothing but a running program (command) on Linux or Unix-like systems.[donotprint]
pstree command details
See running processes in the form of a tree diagram
[/donotprint] The pstree command shows running processes as a tree.
Display running processes as a tree on Linux
The basic syntax is as follows:
pstree [options] pid|user
NOTE: If pid given as a command line argument, start at this PID; default is 1 (init). if user given as a command line argument, show only trees rooted at processes of this user. pstree only accept one argument, and that argument can be either the user or a pid.
Install pstree on Unix
By default the pstree command installed and available on Linux operating systems only. For Unix-like system such as FreeBSD, OS X and others you need to install it using either source code or third party binary repos. You can compile pstree under most unixes, tested are AIX, Linux, HP-UX, A/UX, SunOS, Solaris, (Free|Open|Net)BSD, MacOSX/Darwin, and others. Use wget command to grab the source code: $ wget ftp://ftp.thp.uni-duisburg.de/pub/source/pstree.tar.gz Use tar command to extract files and build the source code with c compiler (cc): $ tar zxvf pstree.tar.gz $ cc -O -o pstree pstree.c ## install binary in /usr/local/bin/ directory ## $ sudo cp pstree /usr/local/bin Sample outputs:
Install pstree on MacOS X
Type the following command: $ brew install pstree
pstree command examples
To see a tree diagram of all the processes currently on the server, type: # pstree # pstree | less # pstree | more Sample outputs:
At the top of the diagram, you will the process init which is the first process that is started when Linux comes on line. init is the parent of all processes on the system, it is executed by the kernel and is responsible for starting all other processes. You can use ASCII characters to draw the tree by passing the -A option: # pstree -A
Display command line arguments
To see the list of command line arguments, pass the -a option: $ pstree -a Sample outputs:
To show PIDS for each process name, pass the -p option: $ pstree -p Sample outputs:
How can I highlight the current process and its ancestors?
Pass the -h option to highlight the current process and its ancestors. Pass -H option highlight the specified process (by name/pid): $ pstree -h OR highlight process with PID # 60093: $ pstree -H 60093 Sample outputs:
How can I view a process by PID or user?
The pstree can either accept PID or username as a command line argument. The syntax is as follows to see info about PID 1313 $ pstree 1313 $ pstree -H 1313 In this following example, display only those tree branches (processes) that have been initiated by a user with a username nixcraft: $ pstree nixcraft
pstree command options
From the pstree(1) command man page:
Show command line arguments.
Use ASCII line drawing characters.
Don’t compact identical subtrees.
Highlight current process and its ancestors.
Highlight this process and its ancestors.
Use VT100 line drawing characters.
Don’t truncate long lines.
Sort output by PID.
Show PIDs; implies -c.
Show uid transitions.
Use UTF-8 (Unicode) line drawing characters.
Display version information.
Show SELinux security contexts.
This tutorials is also available in a quick video format:
The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.