0

This isn't an actual problem - but more of a curious question, when I run while true; do ps aux | grep abc; echo done; done I get the following:

user    29733  0.0  0.0  11748   924 pts/1    R+   20:25   0:00 grep --color=auto abc
done
user    29735  0.0  0.0  11748   920 pts/1    S+   20:25   0:00 grep --color=auto abc
done
user    29737  0.0  0.0  11748   924 pts/1    S+   20:25   0:00 grep --color=auto abc
done
done
done
done
user    29745  0.0  0.0  11748   924 pts/1    R+   20:25   0:00 grep --color=auto abc
done
user    29747  0.0  0.0  11748   924 pts/1    R+   20:25   0:00 grep --color=auto abc
done
user    29749  0.0  0.0  11748   924 pts/1    R+   20:25   0:00 grep --color=auto abc
done
user    29751  0.0  0.0  11748   924 pts/1    R+   20:25   0:00 grep --color=auto abc
done
user    29753  0.0  0.0  11748   924 pts/1    S+   20:25   0:00 grep --color=auto abc
done
user    29755  0.0  0.0  11748   924 pts/1    S+   20:25   0:00 grep --color=auto abc
done
done
user    29759  0.0  0.0  11748   924 pts/1    R+   20:25   0:00 grep --color=auto abc
done
user    29761  0.0  0.0  11748   920 pts/1    R+   20:25   0:00 grep --color=auto abc
done

Sometimes grep doesn't actually see itself in ps aux. Is this just a timing issue between the two processes running? This also happens when I run the commands individually and not in a loop.

This is happening both on my computer and another machine over ssh, but it is happening more frequently on the remote computer (which the output is from).

Ubuntu 14.04

0

3 Answers 3

3

I think this is just timing, as you mention. Commands on pipes run concurrently, you can find more information on In what order do piped commands run?. It might happen more frequently on a machine if you have more/less CPU or more/less processes.

1

It is timing related. You almost always don't want the grep command itself to be part of the output, especially when you're filtering ps and plan on killing the results ;-)

The usual idiom/trick to do that is:

while true; do ps aux | grep [a]bc; echo done; sleep 1; done

Which means grep will still find anything with abc, but the grep command itself won't match because it's literally '[a]bc'...

If you're on linux or a more modern unix

watch -n1 'ps aux | grep [a]bc'

Is a little shorter than the while.. done loop

0

It is indeed timing related.

If you don't want to see your grep command there's two things you can do, depending on your usecase.

If you only need to see the pids resort to:

pgrep -l python

If you need more detail:

ps aux | grep python | grep -v grep

Not the answer you're looking for? Browse other questions tagged .