Cyberithub

Bash command or script to emulate watch utility functionality in Linux/Unix

Advertisements

In this article, we will see bash commands or script that can be used to emulate watch utility functionality in Linux/Unix based systems. It is not very uncommon that you don't have watch utility available in your Linux/Unix based systems. While in some of the Linux/Unix distributions it will be available by default but there are many cases in which it won't be available by default. It could be either due to security restrictions placed on the system or you are unable to install watch utility in your System.

In any case, if you are not able to install and use watch command in your system then you will have an alternate option of creating your own bash command or script to emulate the functionality of watch utility. Here we will see all the different methods which you can use to achieve this task.

Advertisements

 

Bash command or script to emulate watch utility functionality in Linux/Unix

Bash command or script to emulate watch utility functionality in Linux/Unix

Also Read: How to decide the Usage of for loop and while loop in Shell Scripting

Now a days, by default all the famous Linux/Unix distributions contains bash shell where basic utilities like while loop and for loop will be available. So it is easy to think and write a command or script using these loops to emulate the watch utility functionality. Let's see how we can achieve that by using just while and for loop.

Advertisements

 

Method 1: Using While Loop

The first method you can use to emulate watch utility functionality is through while loop. This is the recommended method to use when you don't know the number of times and the total time period for which you need to monitor the output of a command. In this situation, you can just make while condition true and run the actual command. Then you use sleep command to wait for few seconds before it again displays the output and then it will keep on running forever until you stop it manually. This can be further understood through an example.

Let's say you have a kubernetes cluster in which few of the pods are running. Now there is a use case in which you have to restart one pod and you would like to know the status of pods every few seconds till it comes back to running state. This can be done in two ways.

Advertisements

First you restart the pod and then keep running kubectl get pods command till the pod comes back to the running state or you can use the second way in which you write a simple while statement to refresh the output of kubectl get pods command automatically till the pod comes back to the running state. This can be achieved by using below while statement on the command line.

cyberithub@ubuntu:~$ while true;do kubectl get pods;sleep 2;done

The above while loop statement will refresh the output of kubectl get pods command every 2 seconds. If you want to the output to be clearly see then you can also include few new lines in between using echo "\n\n\n\n\n\n" command as shown below.

Advertisements
cyberithub@ubuntu:~$ while true;do kubectl get pods;sleep 2;echo "\n\n\n\n\n\n";done

On a Mac system, you need to use -e switch with echo command to make it work.

[cyberithub@macos1066 ~ % while true;do kubectl get pods;sleep 2;echo -e "\n\n\n\n\n\n";done

Another way you can think of using while statement is by including clear command as well. This will keep removing the previous output and make your screen look like nice and clean hence making the output clearly visible.

cyberithub@ubuntu:~$ while true;do clear;kubectl get pods;sleep 2;done

Instead of using sleep command separately, you can also merge it with while statement and use as shown below.

cyberithub@ubuntu:~$ while sleep 2;do clear;kubectl get pods;done

There is one more way you can think of using while loop is through below statement.

cyberithub@ubuntu:~$ while :;do clear;kubectl get pods;sleep 2;done

If you are thinking to create a bash script and use then you can employ below script to do the Job. You can further modify below script as per your use case scenario.

cyberithub@ubuntu:~$ nano watch.sh
#!/bin/bash

while true
do 
   clear
   kubectl get pods
   sleep 2
done

 

Method 2: Using For Loop

The second method that you can use is through for loop. This method is recommended to be used when you know the number of times and the total time period for which you would like the output of a command to be refreshed. For example if you would like to check the output of kubectl get pods command for 100 number of times and each time the output get refreshed after 5 seconds then you can use below for statement to achieve this task.

cyberithub@ubuntu:~$ for i in {1..100};do kubectl get pods;sleep 5;done

You can also clear the screen before refreshing the output again, just as we used in while statement.

cyberithub@ubuntu:~$ for i in {1..100};do kubectl get pods;sleep 5;echo "\n\n\n";done

On a Mac System, you can use above statement with -e switch in echo command as shown below.

[cyberithub@macos1066 ~ % for i in {1..100};do kubectl get pods;sleep 5;echo -e "\n\n\n";done

If you are looking to create a bash script using for loop then you can create like below.

cyberithub@ubuntu:~$ nano watch.sh
#!/bin/bash

for i in {1..100}
do 
  kubectl get pods
  sleep 5
  echo "\n\n\n"
done

You can further modify above script as per your requirement but overall it should work fine.

Leave a Comment