16

watch ps aux | grep foo is not working: bash: syntax error near unexpected token `}'

How can I fix this or is there even a better way?

blong
  • 209
  • 2
  • 3
  • 13
user237201
  • 171
  • 1
  • 1
  • 3
  • 1
    Are you *sure* that's where the error is coming from? What does `type watch` say? `type grep`? What is the surrounding context (if any) where you're running this? – Michael Homer Jun 22 '17 at 06:20
  • If you've been locked out of your account, please see https://unix.stackexchange.com/help/reset-password. – terdon Jun 22 '17 at 09:13

1 Answers1

32

The problem is that you are watching the wrong thing

  1. watch ps aux | grep foo

implies that you want to grep foo in the output of watch - which unfortunately is silent by itself. It is indeed running ps aux every 2 seconds, but not printing anything on stdout.

However , the following

  1. watch 'ps aux | grep foo'

implies you want to watch if anything matching foo is printed by ps aux . And that is what you want to do.

Hence you would want to go with Option 2

amisax
  • 2,957
  • 17
  • 23
  • 2
    huh, this was working yesterday but today I'm only seeing an apparently truncated list of processes, all owned by root. Running ps aux alone and ps aux | tee -a test.txt both work as expected, but then wrapped by watch as $watch 'ps aux | tee -a test.txt' I only get a short list of processes owned by root. Why, Linux, why? – CCJ Dec 11 '20 at 20:11