Created
September 23, 2018 01:56
-
-
Save koitsu/0e73e1f7775c261309863c632436a0c2 to your computer and use it in GitHub Desktop.
Sane version of the perl code at at https://www.miek.nl/go/#processes
This file contains 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/env perl | |
use strict; | |
use warnings; | |
# %child key = parent PID, value = array of child PIDs | |
use vars qw(%child $pid $parent); | |
open(FH, "ps -axe -o pid,ppid,comm |") or die; | |
while(<FH>) { | |
s/^\s+//g; # Remove any spaces at the beginning of the line | |
tr/ / /s; # Turn sequential spaces into a single space ("squeeze") | |
next if /^PID/; # Skip ps output header | |
($pid, $parent) = (split(/ /, $_))[0,1]; # Space-delimited output; we only care about the first 2 fields | |
push @{ $child{$parent} }, $pid; # Append to the array | |
} | |
close(FH); | |
foreach $pid (sort { $a <=> $b } keys %child) { | |
my $count = scalar @{ $child{$pid} }; | |
printf "Pid %u has %u child%s: [%s]\n", | |
$pid, | |
$count, | |
($count > 1 ? "ren" : ""), | |
join(" ", @{ $child{$pid} }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment