Sunday, January 2, 2011

Monitoring command execution at regular intervals

I use these 2 methods to monitor command execution at regular intervals. One by using the command “watch” and another by using a small while statement.


Using watch command:
 # watch -n <time interval in seconds>  “command” or just  # watch  “command”


Examples:
# watch -n 10 "cat /proc/meminfo | grep -i Mem"      <- Watch Memory utilization at 10 seconds intervals
# watch -n 15 10 “du -sh |  grep /somefilesystem”     <- Watch disk utilization at 15 seconds interval.
# watch "ps aux|grep jboss"                                     <- Watch jboss process with default time interval


Using while statement:
while true; do <commands separated by semicolon>; sleep <time interval in seconds>; done  


Examples:

[adevaraju@hostxyz ~]$ while true; do cat /proc/meminfo | grep -i mem; uptime; echo; sleep 10; done
MemTotal:      4194480 kB
MemFree:         36776 kB
 01:52:51 up 14 days,  9:07,  1 user,  load average: 0.00, 0.00, 0.00


MemTotal:      4194480 kB
MemFree:         36652 kB
 01:53:01 up 14 days,  9:07,  1 user,  load average: 0.00, 0.00, 0.00


MemTotal:      4194480 kB
MemFree:         36776 kB
 01:53:11 up 14 days,  9:07,  1 user,  load average: 0.00, 0.00, 0.00


# while true; do echo Current time is  `date "+%H Hour: %M Minutes: %S Seconds"`; sleep 4; done
Current time is 01 Hour: 47 Minutes: 26 Seconds
Current time is 01 Hour: 47 Minutes: 30 Seconds
Current time is 01 Hour: 47 Minutes: 34 Seconds
Current time is 01 Hour: 47 Minutes: 38 Seconds
Current time is 01 Hour: 47 Minutes: 42 Seconds
Current time is 01 Hour: 47 Minutes: 46 Seconds
Current time is 01 Hour: 47 Minutes: 50 Seconds
Current time is 01 Hour: 47 Minutes: 54 Seconds
Current time is 01 Hour: 47 Minutes: 58 Seconds
Current time is 01 Hour: 48 Minutes: 02 Seconds
Current time is 01 Hour: 48 Minutes: 06 Seconds
#
PS:  “watch” command wouldn’t work properly if the command sets are complex. It will just display the result of first iteration and stay there forever.

5 comments:

  1. Hey! This is my 1st comment here so I just wanted to give a
    quick shout out and say I really enjoy reading through your
    blog posts. Can you suggest any other blogs/websites/forums that deal
    with the same topics? Thank you!
    My site : how to become an mma fighter

    ReplyDelete
  2. Hi, I think your site might be having browser compatibility issues.

    When I look at your blog in Opera, it looks fine but when opening in
    Internet Explorer, it has some overlapping. I just wanted
    to give you a quick heads up! Other then that, excellent
    blog!
    Here is my web-site ... check it out

    ReplyDelete
  3. Howdy, I do think your web site might be having internet browser compatibility issues.
    Whenever I look at your blog in Safari, it looks fine however, if opening in I.

    E., it has some overlapping issues. I just wanted to give you a quick heads up!
    Apart from that, wonderful site!

    Visit my website: FDA approved diet and appetite suppressors in Miami

    ReplyDelete
  4. When some one searches for his essential thing, therefore he/she needs to be available that in detail,
    thus that thing is maintained over here.

    Also visit my blog post; electrician katy

    ReplyDelete
  5. I'm extremely inspired along with your writing talents and also with the layout to your blog. Is this a paid theme or did you modify it yourself? Either way stay up the excellent high quality writing, it's uncommon
    to see a nice blog like this one these days..

    my web page: recomended videos

    ReplyDelete