TranslateProject/sources/tech/20150610 watch--Repeat Linux or Unix Commands Regular Intervals.md
2015-06-13 14:20:30 +08:00

5.4 KiB
Raw Blame History

translating---geekpi

watch - Repeat Linux / Unix Commands Regular Intervals

A server administrator needs to maintain the system and keep it updated and safe. A number of intrusion attempts may happen every day. There are some other activities that maintain their log. These logs are updated regularly. In order to check these updates, the commands are executed repeatedly. For example, for simply reading a file, commands like head, tail, cat etc are used. These commands need to be executed repeatedly. The watch command can be used to repeat a command at regular intervals.

Watch Command

Watch is a simple command, with a few options. The basic syntax of watch command is:

watch [-dhvt] [-n <seconds>] [--differences[=cumulative]] [--help] [--interval=<seconds>] [--no-title] [--version] <command>

Watch command runs the command specified to it after every 2 seconds by default. This time is counted between the completion of command and beginning of next execution. As a simple example, watch command can be used to watch the log updates, The updates are appended at the end of the file, so tail command can be used with watch to see the updates to the file. This command continues to run until you hit CTRL + C to return to the prompt.

Examples

Keep an eye on errors/notices/warning being generated at run time every couple of seconds.

watch tail /var/log/messages

tail messages

Keep an eye on disk usage after specified time interval.

watch df -h

df -h

It is very important for administrators to keep an eye on high I/O wait causing disk operations especially the Mysql transactions.

watch mysqladmin processlist

processlist

Keep an eye on server load and uptime at runtime.

watch uptime

uptime

Keep an eye on queue size for Exim at the time a cron is run to send notices to subscribers.

watch exim -bpc

exim -bpc

1) Iteration delay

watch [-n <seconds>] <command>

The default interval between the commands can be changed with -n switch. The following command will run the tail command after 5 seconds:

watch -n 5 date

date 5 seconds

2) Successive output comparison

If you use -d option with watch command, it will highlight the differences between the first command output to every next command output cumulatively.

watch [-d or --differences[=cumulative]] <command>

Example1

Lets see the successive time outputs extracted using following watch command and observe how the difference is highlighted.

watch -n 15 -d date

First time date is capture when command is executed, the next iteration will be repeated after 15 seconds.

Difference A

Upon the execution of next iteration, it can be seen that all output is exactly same except the seconds have increased from 14 to 29 which is highlighted.

Difference A

Example 2

Lets experience in difference between two successive outputs of “uptime” command repeated by watch.

watch -n 20 -d uptime

uptime

Now the difference between the time is highlighted as well as the three load snapshots as well.

10b

3) Output without title

If you dont want to display extra details about the iteration delay and actual command run by watch then t switch can be used.

watch [-t or --no-title] <command>

Lets see the output of following command as an example.

watch -t date

watch without title

Watch help

Brief details of the watch command can be found by typing the following command in SSH.

watch -h [or --help]

watch help

Watch version

Run the following command in SSH terminal to check the version of watch.

watch -v [--version]

version

BUGS

Unfortunately, upon terminal resize, the screen will not be correctly repainted until the next scheduled update. All --differences highlight-ing is lost on that update as well.

Summary

Watch is a very powerful utility for system administrators because it can be used to monitor, logs, operations, performance and throughput of the system at run time. One can easily format and delay the output of watch utility. Any Linux command / utility or script and be supplied to watch for desired and continuous output.


via: http://linoxide.com/linux-command/linux-watch-command/

作者:Aun Raza 译者:译者ID 校对:校对者ID

本文由 LCTT 原创翻译,Linux中国 荣誉推出