How to fix a problem with standard error screwing up the output of watch

bashunixwatch

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?

Best Answer

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).

Related Question