A system administrator often needs to run a command repeatedly in a certain period of time. Often such tasks can be easily completed with simple cron commands. In most of the cases, this should work, but the shortest period which you can run cron command is every 1 minute. Believe it or not, in many cases, this is too slow.

 

1. Use watch Command

 

Watch is a Linux command that allows you to execute a command or program periodically and also shows your output on the screen. This means that you will be able to see the program output in time. By default, watch re-runs the command/program every 2 seconds. The interval can be easily changed to meet your requirements.

 

Monitor Memory Usage

 

Watch” is extremely easy to use, to test it, you can fire up a Linux terminal right away and type the following command:

# watch free -m

 

The above command will check your system free memory and update the results of the free command every two seconds.

 

Monitor Logged-In Users, Uptime and Load Average

 

Let’s say you want to monitor logged-in users, server uptime and load average output in continuously phase every few seconds, then use the following command as shown:

# watch uptime

 

To exit the command, press CTRL+C.

 

Here, the 'uptime' command will run and display the updated results every 2 seconds by default.

 

Monitor Progress of Copy Command

 

In Linux, while copying files from one location to other using cp command, the progress of data is not shown, to see the progress of data being copied, you can use the watch command along with du -s command to check the disk usage in real-time.

# cp ubuntu-15.10-desktop-amd64.iso /home/rootadminz/ &
# watch -n 0.1 du -s /home/rootadminz/ubuntu-15.10-desktop-amd64.iso 

 

2. Use sleep Command

 

Sleep is often used to debug shell scripts, but it has many other useful purposes as well. For example, when combined with for or while loops, you can get pretty awesome results.

 

If you are new to bash scripting, you can check our guide about bash loops here.

 

In case this is the first time you hear about the "sleep" command, it is used to delay something for a specified amount of time. In scripts, you can use it to tell your script to run command 1, wait for 10 seconds and then run command 2.

 

With the above loops, you can tell bash to run a command, sleep for N amount of seconds and then run the command again.

 

Below you can see examples of both loops:

 

for loop Example

# for i in {1..10}; do echo -n "This is a test in loop $i "; date ; sleep 5; done

 

The above one-liner, will run the echo command and display the current date, total of 10 times, with 5 seconds sleep between executions.

 

Here is a sample output:

 

This is a test in loop 1 Wed Sep 17 20:49:47 EET 2019
This is a test in loop 2 Wed Sep 17 20:49:52 EET 2019
This is a test in loop 3 Wed Sep 17 20:49:57 EET 2019
This is a test in loop 4 Wed Sep 17 20:50:02 EET 2019
This is a test in loop 5 Wed Sep 17 20:50:07 EET 2019
This is a test in loop 6 Wed Sep 17 20:50:12 EET 2019
This is a test in loop 7 Wed Sep 17 20:50:17 EET 2019
This is a test in loop 8 Wed Sep 17 20:50:22 EET 2019
This is a test in loop 9 Wed Sep 17 20:50:27 EET 2019
This is a test in loop 10 Wed Sep 17 20:50:32 EET 2019

 

You can change the echo and date commands with your own commands or script and change the sleep interval per your needs.

 

while loop Example

# while true; do echo -n "This is a test of while loop";date ; sleep 5; done

 

Here is sample output:

 

This is a test of while loopWed Sep 17 20:52:32 EET 2019
This is a test of while loopWed Sep 17 20:52:37 EET 2019
This is a test of while loopWed Sep 17 20:52:42 EET 2019
This is a test of while loopWed Sep 17 20:52:47 EET 2019
This is a test of while loopWed Sep 17 20:52:52 EET 2019
This is a test of while loopWed Sep 17 20:52:57 EET 2019

 

هل كانت المقالة مفيدة ؟ 0 أعضاء وجدوا هذه المقالة مفيدة (0 التصويتات)