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.

Example:
watch tail /var/log/syslog

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 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 setup to monitor the CPU temperature, you can use watch to view that with the sensors command.

watch -n 1 sensors

If you would like to filter this output to only show the temperature output without all of the rest.
You 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 }'”

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 ” “

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s