Using The Watch Command On Linux | Network World| ItSoftNews

Watch is a command on Linux that will repeatedly run commands for you, and it offers some very useful options. One of its basic options is that you can tell watch how long to wait before running the specified command again. For example, if you run the command watch -n 10 date, the watch command will first clear the screen and run the date command right away. After that, it will run the command every 10 seconds until you stop it by hitting control-C. Note that the first line on the screen will indicate the wait time between iterations (every 10 seconds).

$ watch -n 10 date Every 10.0s: date						fedora: Fri, Aug 12 16:32:09 EDT 2022  Fri Aug 12 04:10:11 PM EDT 2022 

The -n option specifies the number of seconds between commands. The default is 2. You might have to look closely to notice the changes in the output.

Each iteration of the chosen command overwrites the prior output. If you want to focus more easily on that part of the output that is changing, you can add the -d option to highlight that portion of the output that is different from the previous output. If you use the d=cumulative setting, any portion of the output that changes will continue to be highlighted with each iteration.

If you use the -g (exit when output changes) option, any change in the output will cause the watch command to stop running. This can be very useful when, for example, you’re waiting for a process to start or end. Once you see the watch output stop, you’ll know that it’s time to move to the next step in your work.

$ watch -n 5 -g "ps -ef | grep install | wc -l" 

In the example above, the ps -ef command output will include both your grep command as well as the install command or script being run. Once the process completes, watch will stop running because the output will change. It will only stop, however, once it completes the next wait cycle.

If you’re waiting for someone to log into or out of a system, you could use the watch command to let you know when they do.

$ watch -g who Every 2.0s: who						fedora: Fri, Aug 12 16:40:00 EDT 2022  shs		tty2		2022-08-12 16:01:03 (tty2) 

A command like this one would repeatedly display the number of processes that are running:

$ watch -n 10 "ps -ef | wc -l" 

Every ten seconds the output would be displayed.

Every 10.0s: pe -ef | wc -l						fedora: Fri, Aug 12 16:32:09 EDT 2022

234

Using the -t option as in the example below will omit the heading line from the watch output.

$ watch -n 10 -t date Fri Aug 12 04:20:10 PM EDT 2022 

When does it end?

The watch command won’t stop running on its own. You can kill it with control-C or have it stop when the output changes by using the -g option. Unfortunately, there is no option for stopping watch when the output stops changing. For some tasks, this would be very handy.

Wrap-up

You can run many different commands using watch. Any time you want to monitor a process, check the size of a log file, look at system performance, or simply wait for a process to finish, there is a watch command that will do that for you. The command also has a helpful man page that can explain its use and the command’s options.

Leave a Reply

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