grep'ping the ps output....

Alex Zbyslaw xfb52 at dial.pipex.com
Mon Dec 12 08:16:58 PST 2005


Eric Schuele wrote:

> Hello,
>
> I am sure this is quite trivial, but...
>
> I have need to determine if an app (firefox, or anything really) is 
> already running before I perform some action.  So I grep the ps 
> output.  However sometimes (many times) that which I'm searching for 
> is present in the output because I am presently grep'ing for it.  But 
> it is not always there.  It seems to be a bit inconsistent. (See below).
>
> I was wondering if someone could explain why it is sometimes there and 
> not other times.  And how I should correctly go about detecting if the 
> process is running before I perform my action.
>
ps | egrep firefox | egrep -v egrep

I can't explain exactly why.  It's clearly a timing thing.  I guess if 
the ps runs quickly enough then perhaps the grep hasn't actually been 
started by the shell.  Or if ps iterates over something, then maybe the 
grep appears in a part of the list that has already been looked at by ps.

--Alex




More information about the freebsd-questions mailing list