Software

How to use the Linux watch command for easier output tracking

Jack Wallen introduces you to a Linux command that can automatically execute a program repeatedly, so you can easily monitor the output for troubleshooting.

There are times when you have to execute a command repeatedly. Let's say you've noticed some odd behavior on a Linux server and you think it might be related to either memory usage or I/O on a server, or maybe you want to monitor changes made within a directory. Thanks to a handy little command called watch, this is actually quite simple.

The watch command comes installed, by default, on nearly all Linux distributions and is a very useful tool to have at the ready. I want to introduce you to watch, so that it can help you with your Linux admin tasks.

What watch does

The watch command allows you to execute a program periodically, displaying the output of the command at specific intervals. Consider watch to be similar to that of using the tail command to view log files, only the output doesn't come from a file, but another command. With this, you can watch the output of the command change over time. Out of the box, the program will execute every two seconds. So if you want to watch the output of the date command change every two seconds, you'd issue it like so:

watch date

The watch command would execute the date command every two seconds, displaying the output in the terminal window (Figure A).

Figure A

Figure A

Watch running the date command.


To exit out of this, hit the [Ctrl]+[c] key combination.

Make it useful

Of course, running the date command with watch isn't very helpful (unless you really want a terminal clock). Let's make this command useful. Say you're sensing an issue with memory on a machine. You could use the free command (displays the amount of used and free memory on a system) in conjunction with watch to get an idea of what's happening with memory. To do that, the command would be:

watch free

The free command will run every 2 seconds, displaying the output in the terminal, so you don't have to keep running the command (Figure B).

Figure B

Figure B

Running free by way of watch.


The one issue with running the basic watch command (especially with a program like free) is that it can be hard to see what changes between execution. Fortunately, we can make that easy with the -d option. If you run watch -d free, every time the free command is executed, anything in the output that changes will be highlighted (Figure C).

Figure C

Figure C

Our output is much easier to read now.


All you have to do now is look for highlights to point to any output that has changed. If you want the highlights to be sticky, you can add the =cumulative option like so:

watch -d=cumulative free

Using the -d=cumulative options makes following the highlights even easier.

But what if you don't need the program to be executed every 2 seconds? You can use the -n option and define (in seconds) how often you want the application to be executed. Say you want the free command to execute every 5 seconds. This command would be:

watch -d -n 5 free

One very good usage for watch is keeping tabs on file changes. Say you have a particularly crucial directory (maybe for client data) and you suspect something is going on with the included files and subdirectories. If you issue the command (where DIRECTORY is the directory to watch):

watch -d=cumulative ls -l /DIRECTORY

The above command will list the contents of that directory every two seconds, highlighting anything that has changed. If you see permission changes (that shouldn't be happening), something might be going on. That's pretty useful.

A simple, yet effective tool

The watch command isn't a deal breaker, but it certainly is an effective tool for troubleshooting or administering a Linux server. To find out more about what watch can do, issue the command man watch and read through the manual page.

Also see:

linuxcommandhero.jpg
Image: Jack Wallen

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks

Free Newsletters, In your Inbox