Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 3 down vote accepted

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

share|improve this answer
Any suggestions for cases in which the command I want to pass to watch has quotes in it? – jonderry Apr 28 '11 at 20:24
@jonderry Two options: Use different quotes (e.g. watch 'something "do something"'), or escape the quotes (watch 'something \'do something\''). – Kromey Apr 28 '11 at 20:25
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 Apr 28 '11 at 20:39
@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: – Kromey Apr 28 '11 at 21:49

You must log in to answer this question.

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