主要是利用查找/proc目录下的相关文件进行查找.
#!/usr/bin/perl
use strict;
use warnings;
#usage: process_grep.pl processname
exit( main(@argv) );
sub main {
my $phash;
my $processname = shift;
my $proc_dir = "/proc";
chdir $proc_dir;
my @pids = glob "[0-9]*";
for my $pid (@pids) {
open( fh, "$pid/cmdline" ) or die "can't $pid file $!";
$phash->{$pid} = $_ while <fh>;
}
delete $phash->{"$$"};
for my $pid ( keys %$phash ) {
print $pid, "\n" if $phash->{$pid} =~ /$processname/;
}
return 0;
}
发表评论