Had to make my own 'ptree' to be used in Darwin hosts. Would not match the original completely, but quite useful nonetheless. The 'ps' command options can be customized to show additional info. The script loads the output of 'ps' to a hash, then prints the process tree of pids passed in command line (the entire tree if no pid's are passed).

#!/usr/bin/perl -wl use strict; use constant {k_maxLength => 100}; sub loadProcList { my ($procListRef) = @_; my (@F, $pid, $ppid, $output); for (qx(ps -eo pid,ppid,etime,command)) { chomp; @F = split; ($pid, $ppid) = @F[0, 1]; $procListRef->{$pid}->{"pid"} = $pid; $procListRef->{$pid}->{"ppid"} = $ppid; $output = join " ", (@F[0, 2 .. $#F]); $procListRef->{$pid}->{"cmd"} = substr($output, 0, &k_maxLengt +h); push @{$procListRef->{$ppid}->{"cpids"}}, $pid; } } sub printer { my ($procListRef, $pid, $flow, $indent) = @_; my ($ppid, $cpid); # initialize if first call; to be set when recursive call $pid = 0 if !defined($pid); $flow = 0 if !defined($flow); $indent = 0 if !defined($indent); if (defined $procListRef->{$pid}) { if (($flow < 1) && (defined($procListRef->{$pid}->{"ppid"}))) +{ $ppid = $procListRef->{$pid}->{"ppid"}; # will not be set for top level process (pid = 0) # could have been easier to check if pid=0, but avoiding h +ardcoding if (exists($procListRef->{$ppid}->{"pid"})) { $indent += printer($procListRef, $ppid, -1, $indent); } } print " " x $indent++ . $procListRef->{$pid}->{"cmd"} if (exi +sts($procListRef->{$pid}->{"cmd"})); # flow > -1 while printing original pid and looking for childr +en if (($flow > -1) && defined($procListRef->{$pid}->{"cpids"})) +{ for $cpid (@{$procListRef->{$pid}->{"cpids"}}) { printer($procListRef, $cpid, 1, $indent); } } return $indent; } } sub main { my %processHash; loadProcList(\%processHash); printer(\%processHash, $_) foreach (@_?@_:1); } main(@ARGV);

Optimizations welcomed

- J

In reply to ptree for darwin by jaimon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.