How can I fix a problem with standard error screwing up the output of watch?

1

Specifically, I am trying to run the following command on both CentOS and Fedora14 (same issue with both)

watch sudo jmap -heap 31945

However, there are a few lines of standard error that screw up the output after jmap is called more than once:

Attaching to process ID 31945, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 14.2-b01

These lines are stripped if I run:

sudo jmap -heap 31945 2> /dev/null

However, if I try:

watch sudo jmap -heap 31945 2> /dev/null

then too many lines are removed (many lines of the actual output are removed).

Why is this happening? Is there a way to fix this?

jonderry

Posted 2011-04-28T19:52:48.523

Reputation: 837

Answers

3

What you want to do is to tell watch that the command it's running should have its output redirected; what you did instead is redirect the output from watch itself.

Try this: watch 'sudo jmap -heap 31945 2> /dev/null'

Note the new quotes -- this is telling watch that that entire thing is the command, not merely the sudo jmap -heap 31945 part, and thus watch is still capable of using standard error itself (which I suspect is the cause of your "lost" lines of output).

Kromey

Posted 2011-04-28T19:52:48.523

Reputation: 4 377

Any suggestions for cases in which the command I want to pass to watch has quotes in it? – jonderry – 2011-04-28T20:24:27.573

@jonderry Two options: Use different quotes (e.g. watch 'something "do something"'), or escape the quotes (watch 'something \'do something\''). – Kromey – 2011-04-28T20:25:38.827

Neither of these works for me. For example, if I run ps -ef | awk -F' ' '{print $2}', I get the pids for all processes. However, watch "ps -ef | awk -F' ' '{print $2}'" gives incorrect field extraction, and watch "ps -ef | awk -F\' \' \'{print $2}\'" leads to an unterminated command. – jonderry – 2011-04-28T20:39:15.630

@jonderry Looks as if I may have lead you astray with the escaping of the single quotes. The first form you tried, however, does work -- after you escape the $ in $2, that is: watch "ps -ef | awk -F' ' '{print \$2}'". See also this page on quoting and escaping in Bash: http://wiki.bash-hackers.org/syntax/quoting

– Kromey – 2011-04-28T21:49:29.067