Commands you might have missed: pstree

Posted by Steve on Fri 18 Jul 2008 at 10:08

If you're using a system which has a lot of users, and you'd like to see who has started a particular script, daemon, or binary, then the pstree utility is very helpful. It draws a tree of all currently running processes - allowing you to see which processes are related.

In much the same way as the tree command isn't likely to be generally useful, this command might seem a little pointless if you're on a single-user machine, and you essentially start everything yourself. But even so it can be helpfull to see where processes have come from.

The most basic usage would look something like this:

skx@taltos:~$  pstree
init-+-apache2---10*[apache2]
     |-atd
     |-clamd
     |-cron
     |-events/0
     |-exim4---exim4
     |-freshclam
     |-getty
     |-gpg-agent
     |-khelper
     |-ksoftirqd/0
     |-kthread-+-aio/0
     |         |-ata/0
     |         |-ata_aux
     |         |-kblockd/0
     |         |-khubd
     |         |-kjournald
     |         |-kmirrord
     |         |-kseriod
     |         |-kswapd0
     |         |-2*[pdflush]
     |         |-xenbus
     |         `-xenwatch
     |-memcached
     |-migration/0
     |-monit---{monit}
     |-munin-node
     |-mysqld_safe-+-logger
     |             `-mysqld---16*[{mysqld}]
     |-pdnsd---4*[{pdnsd}]
     |-python
     |-qpsmtpd-forkser
     |-roundup-server---roundup-server
     |-screen---bash---irssi
     |-ssh-agent
     |-sshd-+-sshd---sshd---bash
     |      `-sshd---sshd---bash---pstree
     |-syslog-ng
     `-watchdog/0

Here we can see several kernel processes running, (aio, ata, kseriod, etc.), several system daemons (syslog-ng, qpsmtpd, etc), as well as the ssh processes open for my current user.

There aren't many ways of customising the output of the display, although you can modify several things such as the display of command line arguments with:

skx@taltos:~$  pstree -a
...
...
  |-rinetd
  |-rpc.statd
  |-screen -S foo
  |   `-bash
  |-screen -S bar
  |   `-bash
  |-ssh-agent
  |-sshd
  |-syslog-ng -p /var/run/syslog-ng.pid
  |-udevd --daemon
  |-vino-session --sm-client-id default5
  |-(watchdog/0)
  |-(watchdog/1)
  |-wnck-applet--oaf-activate-iid=OAFIID:GNOME_Wncklet_Factory
  |-xenconsoled
  |   `-{xenconsoled}
  `-xenstored --pid-file /var/run/xenstore.pid

For all the available options please read the manpage via "man pstree".

If you don't have the pstree command available then you may find it in the psmisc package, and it may be installed by apt-get, or aptitude in the usual manner.

Share/Save/Bookmark


Posted by Anonymous (88.97.xx.xx) on Fri 18 Jul 2008 at 11:36
Alternatively if you supply the 'f' output modifier to standard ps(1) it will print a similar "forest" relationship without having to install any additional packages.

[ Parent | Reply to this comment ]

Posted by Anonymous (190.3.xx.xx) on Fri 18 Jul 2008 at 17:39
yep, the --forest option makes a nice tree (i like it more than the pstree one) but i only found it in the debian/debian-derivatives version of ps, on freebsd the option wasnt available, so pstree was the only way to go.

[ Parent | Reply to this comment ]

Posted by Anonymous (78.49.xx.xx) on Fri 18 Jul 2008 at 17:55
I myself do very often watch my processes with htop instead of top nowadays. Apart from being simpler to use and offering some other nice features it is also able to display the processes in a similar tree view.

Cheers,

Thomas Luzat

[ Parent | Reply to this comment ]

Posted by Anonymous (71.65.xx.xx) on Mon 21 Jul 2008 at 17:06
This is sort of like the -f flag on ps, but according to the man page, it's got some nice options for highlighting.

My personal favorite ps command is 'ps auxf', but I'll have to investigate this. I don't see how useful it would be without the -p flag, though

[ Parent | Reply to this comment ]

Posted by Anonymous (71.191.xx.xx) on Wed 30 Jul 2008 at 14:02
If you're still doing the "basic commands" series I recommend wput, its meant as a companion for wget for uploading to FTP. It takes the folders/files you want uploaded as an argument and the server you want to upload to as a second. It also supports checking to see what files already exist on the server so you can use it to merge changed files.

[ Parent | Reply to this comment ]

User Login

Username:

Password:

[ Advanced Login ]

Register Account

Mail Filtering

Quick Site Search