pgrep -f 'foo' | wc -l -- curiousity!

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

pgrep -f 'foo' | wc -l -- curiousity!

bruce-83
Hey peeps.

From a fed/centos cmdline...

 pgrep -f "foo" | wc -l

will return 0 -- if "foo" doesn't exist in the procTBL, and something
else if "foo" is running.

The curiousity... When I have a simple php

<?php

$f="pgrep -f 'foo' | wc -l";
$t=`$f`;
print $t

?>

$t isn't 0!! -- it's actually 1, or something else if foo is running..

Any ideas why?? I've used the different methods php provides to "run"
shell/.cmdline processes. I get the same results.

Now.. I can do something like
 ps aux | grep 'foo' | grep -v 'grep' | wc -l
and get the correct results within the php as well as the shell.

Didn't find anything via the 'net or SO on this..

Thoughts/comments??
_______________________________________________
users mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: pgrep -f 'foo' | wc -l -- curiousity!

Patrick O'Callaghan-2
On Wed, 2017-08-09 at 14:06 -0400, bruce wrote:

> Hey peeps.
>
> From a fed/centos cmdline...
>
>  pgrep -f "foo" | wc -l
>
> will return 0 -- if "foo" doesn't exist in the procTBL, and something
> else if "foo" is running.
>
> The curiousity... When I have a simple php
>
> <?php
>
> $f="pgrep -f 'foo' | wc -l";
> $t=`$f`;
> print $t
>
> ?>
>
> $t isn't 0!! -- it's actually 1, or something else if foo is running..
>
> Any ideas why?? I've used the different methods php provides to "run"
> shell/.cmdline processes. I get the same results.
>
> Now.. I can do something like
>  ps aux | grep 'foo' | grep -v 'grep' | wc -l
> and get the correct results within the php as well as the shell.
>
> Didn't find anything via the 'net or SO on this..
>
> Thoughts/comments??

I suggest you run the PHP script with the pipe to 'wc' changed to save
the output somewhere, then look at it.

poc
_______________________________________________
users mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: pgrep -f 'foo' | wc -l -- curiousity!

Jon LaBadie
In reply to this post by bruce-83
On Wed, Aug 09, 2017 at 02:06:20PM -0400, bruce wrote:

> Hey peeps.
>
> From a fed/centos cmdline...
>
>  pgrep -f "foo" | wc -l
>
> will return 0 -- if "foo" doesn't exist in the procTBL, and something
> else if "foo" is running.
>
> The curiousity... When I have a simple php
>
> <?php
>
> $f="pgrep -f 'foo' | wc -l";
> $t=`$f`;
> print $t
>
> ?>
>
> $t isn't 0!! -- it's actually 1, or something else if foo is running..
>
> Any ideas why?? I've used the different methods php provides to "run"
> shell/.cmdline processes. I get the same results.
>
> Now.. I can do something like
>  ps aux | grep 'foo' | grep -v 'grep' | wc -l
> and get the correct results within the php as well as the shell.
>
> Didn't find anything via the 'net or SO on this..
>
> Thoughts/comments??

The -f option to pgrep says to look at the command and it arguments.
If you are looking for a command NAMED "foo" and have another command
with "foo" as an argument, "pgrep -f" may list that as well.

Two alternatives, drop the "-f" to look at only the cmd name
or run your pgrep as "pgrep -f '[f]oo'".  That cmd line does
not contain "foo" but still searches for "foo".

jl
--
Jon H. LaBadie                  [hidden email]
_______________________________________________
users mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: pgrep -f 'foo' | wc -l -- curiousity!

Bill Shirley
In reply to this post by Patrick O'Callaghan-2
       -c, --count
              Suppress  normal output; instead print a count of matching processes.  When count does not match any‐
              thing, e.g. returns zero, the command will return non-zero value.


Looks like you would want to use the --count flag instead of piping to wc:
[0:root@elmo CoA3]$ pgrep -f http --count
12
[0:root@elmo CoA3]$ pgrep -f httx --count
0
OR
Check the return code:
EXIT STATUS
       0      One or more processes matched the criteria.
       1      No processes matched.
       2      Syntax error in the command line.
       3      Fatal error: out of memory etc.

$last_line = exec('pgrep -f foo 2>&1', $output, $return_code);
print $output;
print $return_code;


Bill


On 8/9/2017 5:03 PM, Patrick O'Callaghan wrote:
On Wed, 2017-08-09 at 14:06 -0400, bruce wrote:
Hey peeps.

From a fed/centos cmdline...

 pgrep -f "foo" | wc -l

will return 0 -- if "foo" doesn't exist in the procTBL, and something
else if "foo" is running.

The curiousity... When I have a simple php

<?php

$f="pgrep -f 'foo' | wc -l";
$t=`$f`;
print $t

?>

$t isn't 0!! -- it's actually 1, or something else if foo is running..

Any ideas why?? I've used the different methods php provides to "run"
shell/.cmdline processes. I get the same results.

Now.. I can do something like
 ps aux | grep 'foo' | grep -v 'grep' | wc -l
and get the correct results within the php as well as the shell.

Didn't find anything via the 'net or SO on this..

Thoughts/comments??
I suggest you run the PHP script with the pipe to 'wc' changed to save
the output somewhere, then look at it.

poc
_______________________________________________
users mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]


_______________________________________________
users mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]
Loading...