Created
March 30, 2016 01:50
-
-
Save amosshapira/1e4b287140aeb8efbfdb1f2b7779f1da to your computer and use it in GitHub Desktop.
"pstree" simulation for OS X - from http://apple.stackexchange.com/a/137355/56031
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# treeps -- show ps(1) as process hierarchy -- v1.0 [email protected] 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment