Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a bash command that I want to run once a second and keep the output showing on the screen. I could do something like the following:

while "true"; do foo; sleep 1; done

but this creates distracting scrolling. Alternatively, I could do the following:

while "true"; do clear; foo; sleep 1; done

but this causes the output to flash as it is cleared and written. Is there a good way to just have the output of the command overwrite what is on the terminal?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

Have a look at the watch command; something like

watch -n 1 foo

share|improve this answer
    
-bash: watch: command not found.; man watch: No manual entry for watch . –  Daniel Beck Apr 28 '11 at 18:51
    
This looks like it should be what I want, but the output is screwed up. The first call to the command by watch produces good output, and then with each additional call the output becomes increasingly messed up. The alignment progressively worsens until I can't even read it anymore. EDIT: strangely, if I resize the terminal window the output is good again, but still continues to degrade with each passing call. –  jonderry Apr 28 '11 at 18:57
    
It seems that there is some stderror output that may create this problem (I'm running watch sudo jmap -heap <pid>, which begins with four lines of stderror output). If I append 2> /dev/null to the command outside of watch, this strips the output. However, appending 2> /dev/null causes the output to be messed up and missing lines. –  jonderry Apr 28 '11 at 19:04
    
@jonderry: I can't really help because I've never had the problem, but you could try piping the output of your command to cat then watch would be dealing with "catted" stuff which it may find easier to deal with ie watch foo | cat –  Neal Apr 28 '11 at 19:17
    
@Daniel: What operating system are you using? I've just tried on the three Linux operating systems (Opensuse, Fedora and SystemRescueCD) here and they all have it installed - and, as far as I remember, I've not installed it. –  Neal Apr 28 '11 at 19:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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