Continuously Rerun or Repeatedly Running a Linux Command

Often when you are making changing to a system or some configuration and really don’t want to wait for the changes to show up there might be a need to repeatedly run the same command from a Linux terminal until you see the change is live. There are several ways that this can be done, some more complicated than others. If the update is local and would show up in a log file, that file can be watched with a tail -f , or tail -f | grep ‘keyword’.

Many times that this is needed, the change may not show in a log file or be local. A prime example of this is waiting for DNS updates to propagate.

There are a few ways to continuously run a command against a domain to watch for the update to show up.  It can be done with a simple while loop in bash:

# while true; do clear; dig; sleep 10; done

This is simple and does run the command for you every 10 seconds, but there is a better way to save keystrokes and time.

Most Linux systems should have the Watch command.

watch [options] command

watch runs command repeatedly, displaying its output and errors (the
first screenfull). This allows you to watch the program output change
over time. By default, the program is run every 2 seconds. By default, watch will run until interrupted.

This simple command allows all the function of the while loop with a few additional options that can be useful

-d, --differences [permanent]
Highlight the differences between successive updates. Option
will read optional argument that changes highlight to be
permanent, allowing to see what has changed at least once since
first iteration.
-n, --interval seconds
Specify update interval. The command will not allow quicker
than 0.1 second interval, in which the smaller values are

With these two additional flags, watch can update more or less often than the default 2 seconds. Useful when running a whois for a domain to avoid rate limiting from the queried server. The the -d flag will highlight any changes in the results between the current and previous output.

Every 2.0s: dig Tue Aug 7 04:23:22 2018

; <<>> DiG 9.10.3-P4-Ubuntu <<>>
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 39356
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

; EDNS: version: 0, flags:; udp: 1280
; IN A


;; Query time: 1 msec
;; SERVER: xx.xx.1.2#53(xx.xx.1.2)
;; WHEN: Tue Aug 07 04:23:22 DST 2018
;; MSG SIZE rcvd: 55