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
|
|---|