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. Linux watch command can be used to repeat a command at regular intervals.
Watch Command Syntax
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.
- Keep an eye on errors/notices/warning being generated at run time every couple of seconds.
watch tail /var/log/messages
- Keep an eye on disk usage after a specified time interval.
watch 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
- Keep an eye on server load and uptime at runtime.
- Keep an eye on queue size for Exim at the time a cron is run to send notices to subscribers.
watch 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
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>
Let’s see the successive time outputs extracted using the 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.
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.
Let’s experience the difference between two successive outputs of “uptime” command repeated by watch.
watch -n 20 -d uptime
Now the difference between the time is highlighted as well as the three load snapshots as well.
3) Output without title
If you don’t 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>
Let’s see the output of following command as an example.
watch -t date
Brief details of the watch command can be found by typing the following command in SSH.
watch -h [or --help]
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 runtime. One can easily format and delay the output of watch utility. Any linux commands or script and be supplied to watch for desired and continuous output.