Use Watch for repetitive command line actions

Apr 2021

Overview

When you perform an action from the command line, it gets executed just once. So if you type Date, you will see the current time and date at the execution time of the command. But sometimes it is very useful if you are looking for a change to have your command line executed many times, and this can be achieved with a command called Watch.

Using Watch

Using Watch is very simple, but first lets look at a simple command line example.
Lets say I want to see some PODs coming up in Kubernetes, I have to use the following command:

kubectl -n mediakind get pods |grep stream-processor

And as an example, this will give me a single output like so.

mux-bckp-1-stream-processor-mux-65c46756b5-4kvcz 3/5 Running
mux-main-1-stream-processor-mux-7879d98786-nkphj 2/5 Running
stream-processor-config-656fd6d7f4-4wvw4 1/1 Running
stream-processor-simulcrypt-bckp-6d5c94954d-7kp52 1/3 Running
stream-processor-simulcrypt-main-6f8bd6845b-8lc6g 3/3 Running
stream-processor-statmux-bckp-75dc49984-sc9ft 1/3 Running
stream-processor-statmux-main-6d9659dbc6-8nhfv 3/3 Running
stream-processor-ui-connector-789b688bc5-fg6x7 1/1 Running

I can see some of my PODs are up, but not all of them, so I have to keep running this command over and over. Watch will do this for me, and to do this I can put my original command in quotes, and use Watch at the start.

watch "kubectl -n mediakind get pods |grep stream-processor"

Now I will see something similar to the following, and it will automatically refresh every 2 seconds.

Every 2.0s: kubectl -n mediakind get pods |grep stream-processor Wed Apr 21 17:40:43 2021
mux-bckp-1-stream-processor-mux-65c46756b5-4kvcz 5/5 Running
mux-main-1-stream-processor-mux-7879d98786-nkphj 5/5 Running
stream-processor-config-656fd6d7f4-4wvw4 1/1 Running
stream-processor-simulcrypt-bckp-6d5c94954d-7kp52 3/3 Running
stream-processor-simulcrypt-main-6f8bd6845b-8lc6g 3/3 Running
stream-processor-statmux-bckp-75dc49984-sc9ft 3/3 Running
stream-processor-statmux-main-6d9659dbc6-8nhfv 3/3 Running
stream-processor-ui-connector-789b688bc5-fg6x7 1/1 Running

Use CTRL-C to exit Watch.