Perl Monks,
I have a script that looks for process ids running on a system, so I take the output from ps on Red Hat Linux (or tasklist on Windows), and store those values in an array. The odd thing is that I am always missing the last digit of the process id (on Windows and Linux). I have written the regex two different ways, but achieve the same result. Can you see what I am doing wrong?
Method 1
sub getPids{ my ($processName,$owner) = @_; my $ps; my $pid; my $command = "/bin/ps auwwwx | grep $processName | grep -v grep | +grep -v 'sh -c' "; $ps = `$command`; # print $ps; my @lines = split( "\n",$ps); # print @lines; foreach my $line (@lines){ # print $line . "\n"; if ($line =~ qr|$owner\s+(\d*)\w+\s+\d+.+processName=$processName +|){ $pid = $line =~ qr|$owner\s+(\d*)\w+\s+\d+.+processName=$proces +sName|; say "Found $processName in getPids() owned by $owner. PID is $1 +"; #print "$1\n"; }else{ say "Loser"; } } }
Method 2
if ($line =~ qr|$owner\s+(\w+)\w+\s+\d+.+processName=$processName|){ $pid = $line =~ qr|$owner\s+(\w+)\w+\s+\d+.+processName=$processName +|; say "Found $processName in getPids() owned by $owner. PID is $1"; #print "$1\n"; }else{ say "Loser"; }
In reply to Regex and PID issue by JonesyJones
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |