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:
The watch command would execute the date command every two seconds, displaying the output in the terminal window (Figure A).
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:
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).
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).
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.