Saturday, March 15, 2008

Execute a program periodically - BASH

Imagine, your application is generating log files in very short intervals in a particular log directory. You have to check every 3 seconds the count of the number of files in the log directory, so that you can terminate your application when it generates 300 files, also you have to check the time taken in generating those logs (this is for your some testing purpose)

You can use “watch” command which execute a program periodically.

$ watch -n 3 "date; ls | wc -l"

Also you can use “yes” command which basically outputs a string repeatedly until killed.

$ yes "date;ls |wc -l; sleep 3" | sh

And if you are interested in writing a BASH one liner, here it is using while.

$ while :
> do
> date
> ls |wc -l
> sleep 3
> done

* while : #same as while true,

No comments:

© Jadu Saikia