When running a pgrep command with watch it does not only show the actual pgrep output, but also the watch process as well.


watch pgrep -lf donothing

will show 3 processes, although

pgrep -lf donothing

does not show one. Is thee an elegant way to handle this properly with watch to not display the own process of watch?


Using the suggestions by Patrick:

watch pgrep -f '^python donothing.py'

shows me the usage of pgrep (updated every 2 seconds...)

watch pgrep -l 'python donothing.py'

as well (the pgrep commands alone DO work but not in connection with watch).

watch "pgrep 'python donothing.py'"

does not show anything, although the process is there.

  • Adjust the pgrep to not use -f. If you're matching the command name, you should use no dash-argument at all, or -x, not -f. – Patrick Apr 3 '13 at 16:26
  • I tried watch -n 1 pgrep 'python donothing.py' which lists a pgrep error, while the actual pgrep command is fine. I do not understand. – Alex Apr 3 '13 at 16:29
  • pgrep donothing.py or pgrep -x donothing.py – Patrick Apr 3 '13 at 16:32

From your comment you should be doing pgrep donothing.py or pgrep -x donothing.py.

When using pgrep -f it looks for any command which has the matching string in it's name and/or arguments. There is no pretty way of filtering out results. The solution is to match exactly what you're after, so match the process name.
pgrep by default matches just the process name. By default it will search for any process who's name contains the match string. With -x it will search for any process who's name exactly matches the string.

pgrep is also smart enough to understand scripts. If you have a script called foo.sh, it will show up in ps as /bin/sh foo.sh (or something similar). In this case pgrep will instead compare the script name. So you can do pgrep foo.sh or pgrep -x foo.sh.

So in your case, you should be doing pgrep -x donothing.py to get an exact match.

I should note that this only works if the script was launched as /path/to/donothing.py. If you launch it as python donothing.py it will not work.

In this case you need to use a regex like pgrep -f '^python donothing.py'.

The problem you're encountering with watch is quoting. running watch pgrep -f '^python donothing.py' runs pgrep with no arguments. The -f '^python donothing.py' is passed as an argument to watch, not pgrep as intended. The whole pgrep command should be quoted.

watch "pgrep -f '^python donothing.py'"

As another note. If you are going to use this for other stuff, it might safer to do

watch "pgrep -f '(^|\S+/)python donothing.py'"

This will catch the case where the process shows up as something like /usr/bin/python instead of just python.

| improve this answer | |
  • The command I am looking for is python donothing.py, with which your suggestion does not work. – Alex Apr 3 '13 at 16:42
  • @Alex updated answer. See the bit after "EDIT" – Patrick Apr 3 '13 at 16:46
  • Thanks for the update, but the regex expression will not work with watch (it works as pgrep standalone, though). – Alex Apr 3 '13 at 16:48
  • @Alex if it works with pgrep but not watch then youre doing something wrong with watch. Can you update your question with the full command you're trying to run. Always provide specifics :-) – Patrick Apr 3 '13 at 16:52
  • provided specifics, see updated question. – Alex Apr 3 '13 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.