Thread ps ax (5 answers)
Opened by guest at 2009-10-10 16:42

topeg
 2009-10-10 17:34
#126860 #126860
User since
2006-07-10
2611 Artikel
BenutzerIn

user image
wenn du "ps" nichtz nutzen willst oder kannst, musst du selbst in "/proc" wühlen:
ungetestet:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/perl
use strict;
use warnings;

my $cmd='/usr/bin/perl';

my @pids;
for my $proc (glob('/proc/*'))
{
  # alle ordner mit zahlen sind prozesse
  if($proc=~m!/(\d+)$!)
  {
    my $pid=$1;
    # testet ob "exe" vorhanden und die Quelle gleich dem Gesuchten
    if(-f "$proc/exe" && readlink("$proc/exe") eq $cmd)
    { push(@pids,$pid); }
    # kommt der name in die Kommandozeile vor
    elsif(my $cmdline=slurp("$proc/cmdline"))
    { push(@pids,$pid) if($cmdline=~m!^\s*\Q$cmd!s); }
  }
}

print "$cmd is running as (@pids)\n";

sub slurp
{
  my $file=shift;
  return eval{local($/,@ARGV)=(undef,$file); <>}
}


"/proc" solltest du in allen BSDs, Solaris und Linux finden. "/proc/$id/exe" als symbolischer Link auf das Binary sollte auch überall vorhanden sein. Es gibt aber Abweichungen im Format bei "/proc/$id/stat" das Informationen über den Prozess enthält.

View full thread ps ax