Watch is a command on Linux that may repeatedly run instructions for you, and it provides some very helpful choices. One in every of its primary choices is that you could inform watch how lengthy to attend earlier than working the desired command once more. For instance, in the event you run the command watch -n 10 date, the watch command will first clear the display screen and run the date command immediately. After that, it’ll run the command each 10 seconds till you cease it by hitting control-C. Word that the primary line on the display screen will point out the wait time between iterations (each 10 seconds).
$ watch -n 10 date Each 10.0s: date fedora: Fri, Aug 12 16:32:09 EDT 2022 Fri Aug 12 04:10:11 PM EDT 2022
The -n possibility specifies the variety of seconds between instructions. The default is 2. You may need to look carefully to note the modifications within the output.
Every iteration of the chosen command overwrites the prior output. If you wish to focus extra simply on that a part of the output that’s altering, you’ll be able to add the -d possibility to spotlight that portion of the output that’s completely different from the earlier output. Should you use the d=cumulative setting, any portion of the output that modifications will proceed to be highlighted with every iteration.
Should you use the -g (exit when output modifications) possibility, any change within the output will trigger the watch command to cease working. This may be very helpful when, for instance, you’re ready for a course of to start out or finish. When you see the watch output cease, you’ll know that it’s time to maneuver to the following step in your work.
$ watch -n 5 -g "ps -ef | grep set up | wc -l"
Within the instance above, the ps -ef command output will embrace each your grep command in addition to the set up command or script being run. As soon as the method completes, watch will cease working as a result of the output will change. It’ll solely cease, nevertheless, as soon as it completes the following wait cycle.
Should you’re ready for somebody to log into or out of a system, you can use the watch command to let you understand once they do.
$ watch -g who Each 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 show the variety of processes which are working:
$ watch -n 10 "ps -ef | wc -l"
Each ten seconds the output could be displayed.
Each 10.0s: pe -ef | wc -l fedora: Fri, Aug 12 16:32:09 EDT 2022234
Utilizing the -t possibility as within the instance under 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 finish?
The watch command received’t cease working by itself. You possibly can kill it with control-C or have it cease when the output modifications by utilizing the -g possibility. Sadly, there isn’t a possibility for stopping watch when the output stops altering. For some duties, this may be very useful.
Wrap-up
You possibly can run many various instructions utilizing watch. Any time you wish to monitor a course of, examine the dimensions of a log file, have a look at system efficiency, or just watch for a course of to complete, there’s a watch command that may try this for you. The command additionally has a useful man web page that may clarify its use and the command’s choices.
Copyright © 2022 IDG Communications, Inc.