Another looping command that you can use in command prompt is while loop. Used to be my favourite while waiting for some process to finish – usage to monitor that on-going process, as I hate waiting without knowing whats going on – eg. how much records has been transferred etc. etc.

Tested on bash (bourne again shell) shell. Here is the first simplest example:

user1$ env | grep SHELL
SHELL=/bin/bash
user1$ while true
> do
> date
> sleep 5
> done
Mon Feb 27 07:29:24 MYT 2006
Mon Feb 27 07:29:29 MYT 2006
Mon Feb 27 07:29:34 MYT 2006
Mon Feb 27 07:29:39 MYT 2006
Mon Feb 27 07:29:44 MYT 2006
Mon Feb 27 07:29:49 MYT 2006
^C
user1$

Our first example above will display the date and time every 5 seconds using the sleep command to pause every 5 seconds. This is basically an endless loop, the only way to break the loop is by doing a control+c key command (^C), reason simply because we just don’t know how long the records are or just simply to monitor it as long as we like it.

Lets say you are waiting for some long database updating process that also writes into a log files everytime it adds an entry into the database and the sample of the log file is like this:

admin# cat update.log
record ABC12345 successfully added
record ABC13345 failed - duplicate record found
record ABC15446 successfully added
record ABC33455 failed - duplicate record found
record ABC44343 successfully added
record ABC22334 successfully added
...
...

What we can monitor now is how many total records has been added and you can also add how many is failed and how many succeed.

admin# while true
> do
> echo "`date` : `\
cat update.log | wc -l` records so far"
> echo "`grep 'success' update.log | wc -l` success,\
`grep 'failed' update.log | wc -l` failed"
> echo "----------"
> sleep 180
> done
Mon Feb 27 07:29:24 MYT 2006 : 344 records so far
300 success, 44 failed
----------
Mon Feb 27 07:32:24 MYT 2006 : 397 records so far
353 success, 44 failed
----------
...
...

The command sleep 180 will make the execution pause every 3 minute (60 seconds x 3 = 180 seconds).

Thats all for now.

Tags: , , , ,

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>