1

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?

1 Answer 1

5

Have a look at the watch command; something like

watch -n 1 foo

9
  • -bash: watch: command not found.; man watch: No manual entry for watch .
    – Daniel Beck
    Commented Apr 28, 2011 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
    Commented Apr 28, 2011 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
    Commented Apr 28, 2011 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
    Commented Apr 28, 2011 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
    Commented Apr 28, 2011 at 19:25

You must log in to answer this question.

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