JonesyJones has asked for the wisdom of the Perl Monks concerning the following question:
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"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex and PID issue
by stevieb (Canon) on Jun 16, 2016 at 18:32 UTC | |
by JonesyJones (Novice) on Jun 16, 2016 at 18:43 UTC | |
by stevieb (Canon) on Jun 16, 2016 at 18:47 UTC | |
by JonesyJones (Novice) on Jun 16, 2016 at 19:18 UTC | |
by JonesyJones (Novice) on Jun 16, 2016 at 18:57 UTC | |
by Corion (Patriarch) on Jun 16, 2016 at 19:08 UTC | |
by stevieb (Canon) on Jun 16, 2016 at 19:40 UTC | |
|
Re: Regex and PID issue
by AnomalousMonk (Archbishop) on Jun 16, 2016 at 19:06 UTC | |
|
Re: Regex and PID issue
by hippo (Archbishop) on Jun 16, 2016 at 18:22 UTC | |
by JonesyJones (Novice) on Jun 16, 2016 at 18:42 UTC | |
by hippo (Archbishop) on Jun 16, 2016 at 21:02 UTC |