Parce qu'il manque à chaque fois que l'on en a besoin, un pstree minimaliste :
#!/usr/bin/perl -w
## pstree.pl (C) Tom Mattmann
## This program is free software - the BSD license applies.
## Adaptation - 2008/11/06 glb.
use strict;
my %process; # process information
my %pstree; # process hierarchy, HoA
my %psuid; # process uid
my @childs; # number of childs remaining
my $process = shift @ARGV ;
# print process information
sub prnpid {
my ($level,$pid) = @_;
$childs[$level] = $#{$pstree{$pid}};
for (my $x = 0; $x < $level; $x++) {
if ($childs[$x] == 0) {
printf $x==($level -1) ? "`-- " : " ";
} else {
printf $x==($level -1) ? "+-- " : "| ";
}
}
printf "%-5u %s (%s)\n", $pid, $process{$pid}, $psuid{$pid};
if (exists $pstree{$pid}) {
for (sort {$a <=> $b} @{$pstree{$pid}}) {
prnpid($level+1, $_);
$childs[$level]--;
}
}
}
# fetch ps output and build process tree
my @psoutput = `/bin/ps -ef -o ppid,pid,user,args`;
for (@psoutput) {
next unless /^\s*\d+/;
my ($ppid,$pid,$user,$cmd) = $_ =~ /^\s*(\d+)\s+(\d+)\s+(\S+)\s+(.+)/;
$process{$pid} = $cmd;
$psuid{$pid} = $user;
# kernel threads show up with pid=0, do not reference them
push(@{$pstree{$ppid}}, $pid) if ($ppid != 0 && $ppid != $$);
}
# Did the user ask for a particular PID? If not,
# We're simply assuming that the lowest PID belongs to init
my ($first) = defined $process ? ($process) : sort keys %pstree;
exit 0 unless exists $pstree{$first};
# prnpid will recursively call itself
prnpid (0, $first);
Ramassé ici, un peu au hasard, et adapté selon mes goûts.
[EDIT] : Laurent aurait aimé laisser un commentaire, mais ceux-ci sont fermés un peu rapidement à son goût... Donc il faut aller voir chez lui pour avoir la suite :-)