Skip to content

Commit

Permalink
Ask ps for username (getpwuid wont work for remote servers).
Browse files Browse the repository at this point in the history
Added --debug to print lines to STDERR.
  • Loading branch information
acaudwell committed Aug 9, 2010
1 parent f3bbdc7 commit 24db996
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions scripts/gource-ps.pl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@

use strict;
use warnings;
use Getopt::Long qw(GetOptions);

#use Data::Dumper;

my %opt = ();

if(!GetOptions(\%opt, 'debug|d')) {
die("usage: gource-ps.pl [[USER\@]SERVER] | gource --log-format custom - ...\n");
}

my $windows = $^O =~ /win32|msys/i;

if($windows) {
Expand All @@ -34,7 +41,7 @@

sub _proc_list {

my $ps_command = 'ps axo pid,ppid,uid,time,comm';
my $ps_command = 'ps axo pid,ppid,user,time,comm';

my @pslist = $server ? `ssh $user\@$server "$ps_command"` : `$ps_command`;

Expand All @@ -51,13 +58,10 @@ sub _proc_list {
#build process tree
foreach my $line (@pslist) {
$line =~ s/^\s+//;

my ($pid, $ppid, $uid, $time, @command) = split(/\s+/, $line);

my $username = (getpwuid($uid))[0] or next;

my $command = join(' ', @command) || '';
my ($pid, $ppid, $username, $time, @command) = split(/\s+/, $line);

my $command = join(' ', @command) || '';
$command =~ s{^.+/}{}g;

my $proc;
Expand Down Expand Up @@ -168,7 +172,9 @@ sub _make_command_path {
my $proc = $process{$pid};

if($proc->{status}) {
print join('|', $current_time, $proc->{username}, $proc->{status}, _make_command_path($proc)), "\n"
my $output_line = join('|', $current_time, $proc->{username}, $proc->{status}, _make_command_path($proc)). "\n";
print $output_line;
print STDERR $output_line if $opt{debug};
}

#delete if not seen next time
Expand Down

0 comments on commit 24db996

Please sign in to comment.