Skip to content

Instantly share code, notes, and snippets.

@koitsu
Created September 23, 2018 01:56
Show Gist options
  • Save koitsu/0e73e1f7775c261309863c632436a0c2 to your computer and use it in GitHub Desktop.
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
#!/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