Skip to content

Instantly share code, notes, and snippets.

@ceving
Last active January 27, 2016 14:11
Show Gist options
  • Save ceving/daff75b41a0b5739709b to your computer and use it in GitHub Desktop.
Save ceving/daff75b41a0b5739709b to your computer and use it in GitHub Desktop.
Print a process tree on Solaris
#! /usr/bin/perl
##
## Print a process tree on Solaris
##
use strict;
use warnings;
my $l; # process list
my $r; # process relations
open PS, "ps -e -o user= -o pid= -o ppid= -o args= |";
while (<PS>)
{
chomp;
my ($user, $pid, $ppid, $args) = split(" ", $_, 4);
$l->{$pid} = {user=>$user, args=>$args};
$r->{$ppid}->{$pid} = 1;
}
close PS;
sub print_pid {
my ($i, $p) = @_; # indent level, pid
printf "%8s %5s %*s%s\n", $l->{$p}->{user}, $p, $i*2, '', $l->{$p}->{args};
if (exists $r->{$p}) {
for (sort keys %{$r->{$p}}) {
print_pid ($i + 1, $_) unless $_ eq $p;
}
}
}
# On Solaris the first process has not the pid 1 but its own pid
# equals to the parent pid.
my ($a) = grep { exists $r->{$_}->{$_} } keys %$l;
print_pid 0, $a;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment