Did you ever need to monitor live your free memory, or if some files are getting written during the execution of an automated process, or anything else?
I've been in this situation many times, and often I simply repeated the same command over and over ( you know, command-up arrow-enter-up arrow-enter-up arrow-enter etc.) .
Finally I reasoned about that and I found a simple bash one-liner to repeat the same command over and over:
while true; do <command>; sleep 2; clear; done
It is in fact a simple bash while loop. Here is the explanation:
- while true; - execute the loop forever
- do <command>; - replace <command> with which command do you need; I usually use this with very simple commands like ls -l or free. For more complex situations I thin it's better to write a real script.
- sleep 2; - wait 2 seconds before go on with the loop. It's fundamental, because otherwise this command would crash the system - or at least loop so fast to be unusable. Obviously you can put any number after sleep, it's just the seconds the program will wait. I found 2 it's a good compromise, not too fast, not too slow, but it's up to you.
- clear; - it clears the console screen. I find it useful, for I can immediatly notice if there are any changes, for every output is about in the same place. Without it, every output would be printed after the last one and it would be (in my opinion) less clear.
- done; - end of the while loop.
Surely it's not a great innovation, but feel free to use it as you like, if you find it useful!
RELATED POSTS
Labels: bash