On Tue, Jun 11, 2002 at 05:15:17AM -0700, Juli Mallett wrote:
> Hej,
> 
> As some of you may have noticed, I've done some poking of ps(1) lately, and
> this has brought attention of people who have ideas for things that they
> would like to see done to ps(1) :)  The most notable request was for a
> feature I've missed having in our ps(1) for a while, the ability to get a 
> tree of processes printed so you can tell who is whose child, etc. 
> 
> ps(1)'s internals, however, didn't seem quite right to me, but after about
> 10 minutes reading kvm(3) manpages and recalling some tricks with recursive
> programming to produce an N-level tree with as many as N-1 elements, I had
> come up with a simple utility to print out a "process tree".
> 
> You can find the code here:
>         http://people.freebsd.org/~jmallett/.proctree/proctree.c
> 
> And some example output from a cluster machine here:
>         http://people.freebsd.org/~jmallett/.proctree/proctree.out

> Lots of people have given feedback that they don't care much for the \_
> formatting of the tree, and I'm willing to look at patches that provide
> noticably more readable output.

how about this one ?

    1 ?            0  \_ init
 2814 ttyp0        0      \_ sh
 2816 ttyp0        0      |   \_ sh
57423 ?            0      |       \_ sleep
 2596 ?            0      \_ inetd
24834 ?            0      |   \_ rlogind
24838 ttyp0        0      |   |   \_ ksh
24912 ttyp0        0      |   |       \_ ksh
57504 ?            0      |   \_ telnetd
                      ^^^^^^^^^^^^^^^^^^^^^^ command tree
^^^^^^^^^^^^^^^^^^^^ standard ps fields
taken from ast-open `ps -T'. see http://www.research.att.com/~gsf/download/tgz/
for details (maybe one I'll will finish this !@#$%^&* port which is still
broken in some way ?)

for fun, how about a simple awk script like the one in attachment ;^)

Cyrille.
-- 
Cyrille Lefevre                 mailto:[EMAIL PROTECTED]
#!/bin/sh
# was ps -ef
ps axwo "user pid ppid start tt time command" |
awk '
BEGIN {
        getline
}
{
        if (! nchild[$3])
                nchild[$3] = 0
        father[$2] = $3
        children[$3, nchild[$3]] = $2
        nchild[$3] ++
        start[$2] = $4
        tty[$2] = $5
        time[$2] = $6
        cmd[$2] = $7
        for (i = 8; i <= NF; i++)
                cmd[$2] = cmd[$2] " " $i
}
function print_parents(pid) {
        if (pid != 1)
            prefix = "  " print_parents(father[pid])
        else
            prefix = "  "
        printf "%6i %6i %7s %s %s %s\\_ %s\n", \
                pid, father[pid], start[pid], tty[pid], time[pid], \
                substr(prefix, 1, length(prefix)-2), cmd[pid]
        return "  " prefix
}
function print_children (pid, prefix,   child) {
        printf "%6i %6i %7s %s %s %s\\_ %s\n", \
                pid, father[pid], start[pid], tty[pid], time[pid], \
                substr(prefix, 1, length(prefix)-2), cmd[pid]
        if (! nchild[pid])
                return
        for (child = 0; child < nchild[pid] - 1; child ++)
                print_children(children[pid, child], prefix "  | ")
        print_children(children[pid, child], prefix "    ")
}
END {
        if (! whichpid)
                whichpid = 1
        if (! cmd[whichpid])
                exit
        if (father[whichpid])
                prefix = "  " print_parents(father[whichpid])
        else
                prefix = "  "
        print_children(whichpid, prefix)
}' whichpid=$1

Reply via email to