ptree

by Anthony Tonns

If you're like me, you'll find the output of "ptree" in Solaris much more useful than the output of "ps". While it doesn't give you the start time or the terminal it's being run from, the parent process id information is formatted better. In Linux or OpenBSD, there was no easy way to get a tree-like structure that Solaris' "ptree" yielded. Thus, I coded my own. For Linux, I used the entries in /proc. For OpenBSD, the output from "ps ajxwww"

Here is the perl source to Linux ptree.
Here is the perl source to OpenBSD ptree.
Here is the GPL, which is the license for 'ptree'

NOTE:
You can edit the script and change $termwidth to whatever you prefer, or 0 to make it unlimited. $termwidth is how wide the command lines will be (not including the indent, pid or space).

Here's an example of the Linux output (run on my firewall):
Nice ptree formatted output Regular ps formatted output
[root@gwenhwyfar root]# ptree
1 init [3]
  2 [keventd]
  5 [kreclaimd]
  4 [kswapd]
  3 [kapm-idled]
  7 [kupdated]
  6 [bdflush]
  8 [mdrecoveryd]
  73 [khubd]
  437 /sbin/pump -i eth0
  585 syslogd -m 0
  590 klogd -2
  674 /usr/sbin/apmd -p 10 -w 5 -W -P /etc/sysconfig/apm-scripts/apmscript
  700 /usr/local/openssh/sbin/sshd
    30466 /usr/local/openssh/sbin/sshd
      30468 /usr/local/openssh/sbin/sshd
        30469 -bash
          30513 bash
            31664 /usr/bin/perl /usr/local/bin/ptree
  715 /usr/sbin/atd
  727 named -u named
    736 named -u named
      737 named -u named
      738 named -u named
      739 named -u named
  750 xinetd -stayalive -reuse -pidfile /var/run/xinetd.pid
  764 /usr/bin/perl -w /usr/sbin/ddclient -daemon 300
  777 /usr/sbin/dhcpd -cf /etc/dhcpd.conf -lf /var/lib/dhcp/dhcpd.leases -q eth1
  789 /usr/sbin/sockd -D
    823 /usr/sbin/sockd -D
    825 /usr/sbin/sockd -D
    824 /usr/sbin/sockd -D
    826 /usr/sbin/sockd -D
    827 /usr/sbin/sockd -D
    828 /usr/sbin/sockd -D
    2283 /usr/sbin/sockd -D
    2284 /usr/sbin/sockd -D
    2285 /usr/sbin/sockd -D
    2286 /usr/sbin/sockd -D
    2287 /usr/sbin/sockd -D
    2288 /usr/sbin/sockd -D
  795 svscan
    816 supervise log
      830 /usr/local/bin/multilog t s2500000 /var/log/qmail/qmail-send
    817 supervise qmail-smtpd
      834 /usr/local/bin/tcpserver -H -R -v -p -x /etc/tcp.smtp.cdb -u 503 -g 503 10.100.0.1 smtp /var/qmail/bin/qmail-smtpd
    818 supervise log
      829 /usr/local/bin/multilog t s2500000 /var/log/qmail/qmail-smtpd
    815 supervise qmail-send
      822 qmail-send
        831 qmail-lspawn |dot-forward .forward

        833 qmail-clean
        832 qmail-rspawn
  837 /usr/sbin/httpd -DHAVE_PROXY -DHAVE_ACCESS -DHAVE_ACTIONS -DHAVE_ALIAS -DHAVE_ASIS -DHAVE_AUTH -DHAVE_AUTH_ANON -DHAVE_AUTH_DB -DHAV
    23318 /usr/sbin/httpd -DHAVE_PROXY -DHAVE_ACCESS -DHAVE_ACTIONS -DHAVE_ALIAS -DHAVE_ASIS -DHAVE_AUTH -DHAVE_AUTH_ANON -DHAVE_AUTH_DB -DHAV
    23317 /usr/sbin/httpd -DHAVE_PROXY -DHAVE_ACCESS -DHAVE_ACTIONS -DHAVE_ALIAS -DHAVE_ASIS -DHAVE_AUTH -DHAVE_AUTH_ANON -DHAVE_AUTH_DB -DHAV
    23319 /usr/sbin/httpd -DHAVE_PROXY -DHAVE_ACCESS -DHAVE_ACTIONS -DHAVE_ALIAS -DHAVE_ASIS -DHAVE_AUTH -DHAVE_AUTH_ANON -DHAVE_AUTH_DB -DHAV
    23316 /usr/sbin/httpd -DHAVE_PROXY -DHAVE_ACCESS -DHAVE_ACTIONS -DHAVE_ALIAS -DHAVE_ASIS -DHAVE_AUTH -DHAVE_AUTH_ANON -DHAVE_AUTH_DB -DHAV
    23320 /usr/sbin/httpd -DHAVE_PROXY -DHAVE_ACCESS -DHAVE_ACTIONS -DHAVE_ALIAS -DHAVE_ASIS -DHAVE_AUTH -DHAVE_AUTH_ANON -DHAVE_AUTH_DB -DHAV
    23322 /usr/sbin/httpd -DHAVE_PROXY -DHAVE_ACCESS -DHAVE_ACTIONS -DHAVE_ALIAS -DHAVE_ASIS -DHAVE_AUTH -DHAVE_AUTH_ANON -DHAVE_AUTH_DB -DHAV
    23321 /usr/sbin/httpd -DHAVE_PROXY -DHAVE_ACCESS -DHAVE_ACTIONS -DHAVE_ALIAS -DHAVE_ASIS -DHAVE_AUTH -DHAVE_AUTH_ANON -DHAVE_AUTH_DB -DHAV
    23323 /usr/sbin/httpd -DHAVE_PROXY -DHAVE_ACCESS -DHAVE_ACTIONS -DHAVE_ALIAS -DHAVE_ASIS -DHAVE_AUTH -DHAVE_AUTH_ANON -DHAVE_AUTH_DB -DHAV
  857 proftpd (accepting connections)
  897 /home/squid/bin/squid -D
    899 (squid) -D
      905 (dnsserver) -D
      904 (dnsserver) -D
      907 (dnsserver) -D
      906 (dnsserver) -D
      908 (dnsserver) -D
      921 (unlinkd)
  909 xfs -droppriv -daemon
  957 dnetc -quiet
  973 /sbin/mingetty tty4
  972 /sbin/mingetty tty3
  971 /sbin/mingetty tty2
  975 /sbin/mingetty tty6
  974 /sbin/mingetty tty5
  1242 login -- root
    22369 -bash
  16741 crond
    31632 CROND
      31633 /bin/bash /usr/bin/run-parts /etc/cron.hourly
        31635 awk -v progname=/etc/cron.hourly/sysstat progname {

  31636 /bin/sh /usr/lib/sa/sa1 600 6
    31638 /usr/lib/sa/sadc 600 6 /var/log/sa/sa22
[root@gwenhwyfar root]# ps -aef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Jul08 ?        00:00:05 init [3] 
root         2     1  0 Jul08 ?        00:00:00 [keventd]
root         3     1  0 Jul08 ?        00:00:00 [kapm-idled]
root         4     1  0 Jul08 ?        00:00:48 [kswapd]
root         5     1  0 Jul08 ?        00:00:00 [kreclaimd]
root         6     1  0 Jul08 ?        00:00:06 [bdflush]
root         7     1  0 Jul08 ?        00:00:00 [kupdated]
root         8     1  0 Jul08 ?        00:00:00 [mdrecoveryd]
root        73     1  0 Jul08 ?        00:00:00 [khubd]
root       437     1  0 Jul08 ?        00:00:00 /sbin/pump -i eth0
root       585     1  0 Jul08 ?        00:00:01 syslogd -m 0
root       590     1  0 Jul08 ?        00:00:00 klogd -2
root       674     1  0 Jul08 ?        00:00:00 /usr/sbin/apmd -p 10 -w 5 -W -P /etc/sysconfig/apm-scripts/apmscript
root       700     1  0 Jul08 ?        00:00:14 /usr/local/openssh/sbin/sshd
daemon     715     1  0 Jul08 ?        00:00:00 /usr/sbin/atd
named      727     1  0 Jul08 ?        00:00:00 named -u named
named      736   727  0 Jul08 ?        00:00:00 named -u named
named      737   736  0 Jul08 ?        00:00:16 named -u named
named      738   736  0 Jul08 ?        00:00:02 named -u named
named      739   736  0 Jul08 ?        00:00:01 named -u named
root       750     1  0 Jul08 ?        00:00:00 xinetd -stayalive -reuse -pidfile /var/run/xinetd.pid
root       764     1  0 Jul08 ?        00:02:02 /usr/bin/perl -w /usr/sbin/ddclient -daemon 300
root       777     1  0 Jul08 ?        00:00:00 /usr/sbin/dhcpd -cf /etc/dhcpd.conf -lf /var/lib/dhcp/dhcpd.leases -q eth1
sockd      789     1  0 Jul08 ?        00:00:00 /usr/sbin/sockd -D
root       795     1  0 Jul08 ?        00:00:01 svscan
root       815   795  0 Jul08 ?        00:00:00 supervise qmail-send
root       816   795  0 Jul08 ?        00:00:00 supervise log
root       817   795  0 Jul08 ?        00:00:00 supervise qmail-smtpd
root       818   795  0 Jul08 ?        00:00:00 supervise log
qmails     822   815  0 Jul08 ?        00:00:00 qmail-send
sockd      823   789  0 Jul08 ?        00:00:00 /usr/sbin/sockd -D
sockd      824   789  0 Jul08 ?        00:00:00 /usr/sbin/sockd -D
sockd      825   789  0 Jul08 ?        00:00:00 /usr/sbin/sockd -D
sockd      826   789  0 Jul08 ?        00:00:00 /usr/sbin/sockd -D
sockd      827   789  0 Jul08 ?        00:00:00 /usr/sbin/sockd -D
sockd      828   789  0 Jul08 ?        00:01:53 /usr/sbin/sockd -D
qmaill     829   818  0 Jul08 ?        00:00:00 /usr/local/bin/multilog t s2500000 /var/log/qmail/qmail-smtpd
qmaill     830   816  0 Jul08 ?        00:00:00 /usr/local/bin/multilog t s2500000 /var/log/qmail/qmail-send
root       831   822  0 Jul08 ?        00:00:00 qmail-lspawn |dot-forward .forward?|preline procmail
qmailr     832   822  0 Jul08 ?        00:00:00 qmail-rspawn
qmailq     833   822  0 Jul08 ?        00:00:00 qmail-clean
qmaild     834   817  0 Jul08 ?        00:00:00 /usr/local/bin/tcpserver -H -R -v -p -x /etc/tcp.smtp.cdb -u 503 -g 503 10.100.0.1 s
root       837     1  0 Jul08 ?        00:00:02 /usr/sbin/httpd -DHAVE_PROXY -DHAVE_ACCESS -DHAVE_ACTIONS -DHAVE_ALIAS -DHAVE_ASIS -
ftp        857     1  0 Jul08 ?        00:00:00 proftpd (accepting connections)
root       897     1  0 Jul08 ?        00:00:00 /home/squid/bin/squid -D
squid      899   897  0 Jul08 ?        00:07:44 (squid) -D
squid      904   899  0 Jul08 ?        00:00:08 (dnsserver) -D
squid      905   899  0 Jul08 ?        00:00:00 (dnsserver) -D
squid      906   899  0 Jul08 ?        00:00:00 (dnsserver) -D
squid      907   899  0 Jul08 ?        00:00:00 (dnsserver) -D
squid      908   899  0 Jul08 ?        00:00:00 (dnsserver) -D
xfs        909     1  0 Jul08 ?        00:00:00 xfs -droppriv -daemon
squid      921   899  0 Jul08 ?        00:00:04 (unlinkd)
dnetc      957     1 99 Jul08 ?        14-09:13:57 dnetc -quiet
root       971     1  0 Jul08 tty2     00:00:00 /sbin/mingetty tty2
root       972     1  0 Jul08 tty3     00:00:00 /sbin/mingetty tty3
root       973     1  0 Jul08 tty4     00:00:00 /sbin/mingetty tty4
root       974     1  0 Jul08 tty5     00:00:00 /sbin/mingetty tty5
root       975     1  0 Jul08 tty6     00:00:00 /sbin/mingetty tty6
root      1242     1  0 Jul08 tty1     00:00:00 login -- root     
sockd     2283   789  0 Jul09 ?        00:00:00 /usr/sbin/sockd -D
sockd     2284   789  0 Jul09 ?        00:00:00 /usr/sbin/sockd -D
sockd     2285   789  0 Jul09 ?        00:00:00 /usr/sbin/sockd -D
sockd     2286   789  0 Jul09 ?        00:00:00 /usr/sbin/sockd -D
sockd     2287   789  0 Jul09 ?        00:00:00 /usr/sbin/sockd -D
sockd     2288   789  0 Jul09 ?        00:00:00 /usr/sbin/sockd -D
root     16741     1  0 Jul16 ?        00:00:00 crond
root     22369  1242  0 Jul20 tty1     00:00:00 -bash
apache   23316   837  0 Jul21 ?        00:00:00 /usr/sbin/httpd -DHAVE_PROXY -DHAVE_ACCESS -DHAVE_ACTIONS -DHAVE_ALIAS -DHAVE_ASIS -
apache   23317   837  0 Jul21 ?        00:00:00 /usr/sbin/httpd -DHAVE_PROXY -DHAVE_ACCESS -DHAVE_ACTIONS -DHAVE_ALIAS -DHAVE_ASIS -
apache   23318   837  0 Jul21 ?        00:00:00 /usr/sbin/httpd -DHAVE_PROXY -DHAVE_ACCESS -DHAVE_ACTIONS -DHAVE_ALIAS -DHAVE_ASIS -
apache   23319   837  0 Jul21 ?        00:00:00 /usr/sbin/httpd -DHAVE_PROXY -DHAVE_ACCESS -DHAVE_ACTIONS -DHAVE_ALIAS -DHAVE_ASIS -
apache   23320   837  0 Jul21 ?        00:00:00 /usr/sbin/httpd -DHAVE_PROXY -DHAVE_ACCESS -DHAVE_ACTIONS -DHAVE_ALIAS -DHAVE_ASIS -
apache   23321   837  0 Jul21 ?        00:00:00 /usr/sbin/httpd -DHAVE_PROXY -DHAVE_ACCESS -DHAVE_ACTIONS -DHAVE_ALIAS -DHAVE_ASIS -
apache   23322   837  0 Jul21 ?        00:00:00 /usr/sbin/httpd -DHAVE_PROXY -DHAVE_ACCESS -DHAVE_ACTIONS -DHAVE_ALIAS -DHAVE_ASIS -
apache   23323   837  0 Jul21 ?        00:00:00 /usr/sbin/httpd -DHAVE_PROXY -DHAVE_ACCESS -DHAVE_ACTIONS -DHAVE_ALIAS -DHAVE_ASIS -
root     30466   700  0 21:05 ?        00:00:00 /usr/local/openssh/sbin/sshd
tony     30468 30466  0 21:05 ?        00:00:04 /usr/local/openssh/sbin/sshd
tony     30469 30468  0 21:05 pts/1    00:00:00 -bash
root     30513 30469  0 21:09 pts/1    00:00:01 bash
root     31632 16741  0 22:01 ?        00:00:00 CROND
root     31633 31632  0 22:01 ?        00:00:00 /bin/bash /usr/bin/run-parts /etc/cron.hourly
root     31635 31633  0 22:01 ?        00:00:00 awk -v progname=/etc/cron.hourly/sysstat progname {?????   print progname ":\n"?????
root     31636     1  0 22:01 ?        00:00:00 /bin/sh /usr/lib/sa/sa1 600 6
root     31638 31636  0 22:01 ?        00:00:00 /usr/lib/sa/sadc 600 6 /var/log/sa/sa22
root     31665 30513  0 22:07 pts/1    00:00:00 ps -aef

Tonns.org Homepage