Linux Watch Command

Linux Watch Command 

 

       Watch Command is used to run commands at regular intervals like 10 sec,1 mins, etc . This is alternate to cron command. 

watch [option] [command]

Where:

  • [option]: Adding an option changes the way the watch command behaves. Available options are listed below.
  • [command]: A user-defined command you want to run repeatedly.

The watch command options include:

-n--intervalAllows you to specify the interval between output updates.
-d--differencesHighlights the differences between output updates.
-g--chgexitExits the watch command when the output of the user-defined command changes.
-t--no-titleRemoves the header showing the interval, command, and current time and date.
-b--beepPlays a sound alert (beep) if the command exits with an error.
-p--preciseAttempts to run the command after the exact number of seconds defined by the --interval option.
-e--errexitStops output updates on error and exits the command after a key press.
-c--colorInterprets ANSI color and style sequences.
-x--execPasses the user-defined command to exec, reducing the need for extra quoting.
-w--no-linewrapTurns off line wrapping and truncates long lines instead.
-h--helpDisplays help text and exits.
-v--versionDisplays version information and exits.

Linux Watch Command Examples

Here are some of the ways you can use the watch command options to achieve different results:

Run Command with a Custom Interval

Set a custom interval to run a user-defined command and show the output by using the -n or --interval option:

watch -n [interval in seconds] [command]

For instance, to display the system time and date every 5 seconds, run:

watch -n 5 date

Use the -n option to set a custom interval


For ORACLE DBA 

            

       watch -n 30 dcli -l root -g /u01/onecommand/dbs_group uptime

Note: Above Command will run for every 30 sec 


To highlight the difference between previous and current output 

watch -n 5 -d date