Watch: Repeat Unix Commands or Shell-Scripts every N seconds

Question: Sometimes I want to repeat a command (or shell-script) frequently (every few seconds). Is there a better way to execute a unix command every n seconds (instead of keep typing the same command manually)?

Answer: Using watch command you can execute a command and view it’s output every few seconds. This is very helpful while monitoring memory usage, disk usage etc.,

You can execute any Unix command using watch command. Watch command will be executed until you terminate it either by CTRL+C or kill the process.

Example 1: Watching for a file creation using watch ls

When you are expecting a file to be created by a background process, or when you are expecting a log file to keep growing in size, you might end-up doing ls command multiple times. Instead, you can use watch command, which will execute the ls command every 2 seconds as shown below.

Hi, Thanks for the guide , i was looking for something like this and i was using a while loop to do this.
[…]Answer: Using watach command you can execute […]
“watach” is mistyped i think, please change it 😉

I would like to use this command to run in an external hard drive I use to backup as it constantly spins down and unmounts. But it is only plugged in once a week for backups. How would I write such a command? It typically mounts as sdb1 and sdb2 or usb0 and usb1 (it is 2 partitions). Thank you, I find this site very interesting but a little advanced for me.

About The Geek Stuff

My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about Ramesh Natarajan and the blog.

Contact Us

Email Me :
Use this Contact Form to get in touch me with your comments, questions or suggestions about this site. You can also simply drop me a line to say hello!.