Command Line Basics – watch

December 9, 2013 by
Filed under: bash, command line, HowTo, linux 

There are several log files in a Linux system. Keeping an eye on these log files can be one of the important tasks of a Linux System administrator. You can easily view the end of a log file using the tail command. But if you want to monitor that file all day long it's pretty tedious to enter the tail command every few minutes to check on that log file. You could write a short script  with an infinite loop to check the file periodically, but it turns out that there is already a program to handle repetitive tasks for you.

The Linux watch Command

The watch command in Linux provides a way to handle repetitive tasks. By default watch will repeat the command that follows it every two seconds. As you can imagine, watch is a great tool to keep an eye on log files. Here's an example.

watch tail /var/log/syslog

In order to stop the command execution, just use the standard kill sequence, [Ctrl]+C.

watch syslog

Using the Linux watch command to monitor the syslog

Affiliate Link

You can change the time interval by issuing the -n switch and specifying the interval in seconds. To check the log file every 10 seconds, try this.

watch -n 10 tail /var/log/syslog

The Linux watch Command with a Pipe

The watch command isn't limited to viewing log files. It can be used to repeat any command you give it. If you have your system set up to monitor the CPU temperature, you can use watch to view that with the sensors command.

watch -n 1 sensors

The output on my netbook looks like this:

Adapter: Virtual device
temp1:        +45.0°C  (crit = +100.0°C)

I'd like to filter this output to only show the temperature output without all of the rest.

I can use this command to view it one time.

sensors | grep temp | awk '{ print $2 }'

Keep in mind that the watch command will repeat the first command that is sees. Care must be taken when pipelining one command to the next. This can be managed by placing your command pipeline inside quotes.

watch -n1 "sensors | grep temp | awk '{ print $2 }'"
Linux watch with pipe

Using the Linux watch command with a pipe

Use watch as a clock

As you've probably noticed by now, the watch command shows the time that the command was executed in the upper right corner of the terminal window. We can use watch as a simple clock by passing an empty command line argument. We can just enclose a space in quotes to act as the empty command.

watch -n 1 " "

So you can see, this gives another meaning for the command name, watch. You can use it just like your wrist watch.

So now you know how to use the Linux watch command. What repetitive tasks will you use it to handle?



3 Responses to “Command Line Basics – watch”

  1. trouffman says:

    You coule also use tail -F file...
    Auto refresh and display even when the file change.

    • Linerd says:

      You are exactly correct. I was struggling to come up with a good example when I wrote this, so I used the tail example. tail -F is certainly more appropriate for that particular task.

      Thanks for your comment.

  2. Completely agree. `watch` proved to be one great tool that I personally find it extremely helpful. It is also cool when you add the '-d' argument, it highlight the differences between successive updates so you can actually easily notice when something has changed.

    for example:

    ## watch -d -n1 "sensors | grep temp | awk '{ print $2 }'"


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>