El siguiente script en perl que he llamado 'treeps' que debe hacer exactamente eso; funciona en linux (Sci Linux 6) + X (10.6, 10.9)
Ejemplo de salida:
$ ./treeps
|_ 1 /sbin/launchd
|_ 10 /usr/libexec/kextd
|_ 11 /usr/sbin/DirectoryService
|_ 12 /usr/sbin/notifyd
|_ 118 /usr/sbin/coreaudiod
|_ 123 /sbin/launchd
[..]
|_ 157 /Library/Printers/hp/hpio/HP Device [..]
|_ 172 /Applications/Utilities/Terminal.app [..]
|_ 174 login -pf acct
|_ 175 -tcsh
|_ 38571 su - erco
|_ 38574 -tcsh
Aquí está el código..
#!/usr/bin/perl
# treeps -- show ps(1) as process hierarchy -- v1.0 erco@seriss.com 07/08/14
my %p; # Global array of pid info
sub PrintLineage($$) { # Print proc lineage
my ($pid, $indent) = @_;
printf("%s |_ %-8d %s\n", $indent, $pid, $p{$pid}{cmd}); # print
foreach my $kpid (sort {$a<=>$b} @{ $p{$pid}{kids} } ) { # loop thru kids
PrintLineage($kpid, " $indent"); # Recurse into kids
}
}
# MAIN
open(FD, "ps axo ppid,pid,command|");
while ( <FD> ) { # Read lines of output
my ($ppid,$pid,$cmd) = ( $_ =~ m/(\S+)\s+(\S+)\s(.*)/ ); # parse ps(1) lines
$p{$pid}{cmd} = $cmd;
# $p{$pid}{kids} = (); <- this line is not needed and can cause incorrect output
push(@{ $p{$ppid}{kids} }, $pid); # Add our pid to parent's kid
}
PrintLineage(1, ""); # recurse to print lineage starting with pid 1