Wednesday, October 20, 2010

Schedule Scripts With Interval of One Second

Cron jobs with default setting has default scheduling time Of 1MIN.
Below is the trick to operate in seconds.
#! /bin/bash
PROCNAME=’/usr/bin/xyz.rb’
PIDS=`ps -efa | grep $PROCNAME | grep -v grep | awk ‘{ print $2 }’`
for ff in $PIDS
do
echo “$ff” > /tmp/w
done
if [ -f "/tmp/w" ] ; then
pid=$(cat /tmp/w)
if [ "$pid" == "" ] ;then
/usr/local/bin/ruby /usr/bin/xyz.rb
else
echo “Already running $pid”
fi
rm -rf /tmp/w
else
/usr/local/bin/ruby /usr/bin/xyz.rb &
fi
Where xyz.rb is simple rb program in loop
require ‘rubygems’
require ‘daemons’
loop do
system(‘/usr/bin/Server_get |xargs >/var/www/html/text.txt’)
system(‘/usr/bin/get_app_stats |xargs >/var/www/html/app_stat.txt’)
sleep (25)
end
At the last  add it to crontab
* * * * * /usr/bin/fast_cron
cheers  its much useful in fast messaging,emailing campaigns .

No comments: