≡ Menu

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.

$ watch ls
Every 2.0s: ls                                             Thu Apr 15 16:11:58 2010
flash-plugin-10.0.45.2-release.i386.rpm
install_flash_player_10_linux.deb

The first line of the watch command output contains the following header information:

  • Every 2.0s : Time interval. i.e ls will be executed every 2 seconds.
  • ls : Unix command that is getting executed every 2 seconds.
  • Thu Apr 15 16:11:58 2010 : the current date & time.

The rest of the lines are the output of the command that watch is executing. In this example, it displays the output of ls command.

Example 2: Display used & free memory details (with differences highlighted)

The -d option highlights the difference between successive updates.

$ watch -d free -om
Every 2.0s: free -om                               Thu Apr 15 16:11:59 2010

             total	 used       free     shared    buffers     cached
Mem:           992        878        114          0        135        379
Swap:         4095          8       4087

If the memory output doesn’t change on your system, try the -d option with date command to see how it works.

$ watch -d date

Note: Did you know that you can highlight the changes in the top command output?

Example 3: Display available disk space (repeat every 10 seconds)

By default watch command uses 2 second interval, you can change it using -n option. The following example executes df -h command every 10 seconds.

$ watch -n 10 df -h
Every 10.0s: df -h                                Thu Apr 15 16:12:26 2010

Filesystem            Size  Used Avail Use% Mounted on
/dev/sda7              30G  8.6G   20G  31% /
tmpfs                 497M  964K  496M   1% /dev/shm
/dev/sda6             194M   22M  163M  12% /boot

Example 4: Suppress watch header in the output

You can supress the 1st line of the watch command output using option -t as shown below.

$ watch -t ls
flash-plugin-10.0.45.2-release.i386.rpm
install_flash_player_10_linux.deb
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Farhad Hedayati-Fard May 14, 2010, 6:09 am

    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 😉

  • cmcanulty May 14, 2010, 7:50 am

    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.

  • GraphiteCube May 14, 2010, 8:28 am

    “Answer: Using watach command you can execute a command and view it’s output every few seconds.”

    It should be “Answer: Using watch command you can…”?

  • Ramesh Natarajan May 14, 2010, 8:57 am

    @Farhad, @GraphiteCube,

    Thanks for catching the typo. I’ve changed it.

  • asv May 14, 2010, 11:55 am

    # while true; do ls; sleep 2; done

  • NetSpider May 15, 2010, 7:02 am

    for FreeBSD is “gnu-watch” utility

  • noam September 4, 2012, 1:34 am

    Very helpful, Thanks!

  • Anil September 17, 2013, 1:29 pm

    Is there any way to do the free command for every one sec and for 1hour?

    I am able to do vmstat and mpstat like below, but I don’t know how to do for free command

    mpstat 1 550
    vmstat 1 550
    free -bs 1 (I don’t how/where to specify 550 limit)

  • Rob November 5, 2013, 6:00 am

    Can’t get any of them to work, I am such an idiot.

  • ROCK September 4, 2014, 5:19 pm

    How should I store result into a file?

    watch ‘uptime’ ………….. result

  • NetSpider September 9, 2014, 4:31 am

    watch `uptime` > uptime.txt
    but you should manually interrupt the command because it will run endless.

  • NetSpider September 9, 2014, 4:32 am

    watch uptime > uptime.txt