Shell Script Run Interval to be dynamic

Hi All.

I have a script which has to be run periodically. The frequency of its run will be decided by a Database stored value PollRate.
e.g. If PollRate value is 300secs, then the script should be executed every 5 minutes, if it's 1500secs, it should execute every 15 minutes.

Is there anyway, by which the crontab entry for the script can be modified everytime the DB value changes.

If not, then I think I will have to run the script in an infinite loop and at the end of the loop would have to specify a sleep for the value of the PollRate field.
The Problem with the above approach is that everytime the script exits abruptly, it will have to be maunally restarted.

Please suggest a simple and robust solution.

Thanks,
Rahul.

Let's say that your script is /home/joe/bin/something. Somehow you need a way to get the interval into a variable called INTERVAL. So then:

#! /usr/bin/ksh
exec >/dev/null 2>&1
INTERVAL=$(get.interval.somehow)
echo /home/joe/bin/something | at now +"$((INTERVAL/60))" minutes

as the first few lines of /home/joe/bin/something will cause the script to reschedule itself based on the current interval. This is untested, but I think it should work.

i was going to suggest a way to edit the crontabs but Perderabo's at solution might actually be better for your situation ... with a slight modification ...

to get your script to start automatically after booting up, create a script in /etc/rc3.d to do that for you ... please see other scripts in that directory for proper formatting and naming and also "man init" ...

... then go with Perderabo's script ...

doing so will allow you the flexibility to change the polling intervals as required without having to manually start the process after a reboot ...

Hi Perderabo,

I dint get how can I use this in my script.

This is the cron for my current script
00,05,10,15,20,25,30,35,40,45,50,55 * * * * . $HOME/db-bin/controller.sh

This script is currently every 5 minutes.

In this script I call, three other shell scripts.

NOw, How do i schedule a cron. And if from the above method, no cron is needed, how will i take care if the script has exited abruptly?

What I thought was, I can scehdule a cron for every minute.i.e the Interval cannot be less than a minute.
Then I can log the last_run_time in the database and run it if the interval from the last run time is completed.

Thanks,
Rahul.

My idea does not involve cron (except that at command needs cron running). I explained it as well as I could. The only murky part is how know when to rerun the script and I do not understand your scheme so I can't elaborate on that.

how bt executing a script at the end of your script which sets the env variable INTERVAL
dunno if cron or at can read env variables as its parameter, but if yes, then i guess this should do the trick

try these steps:
(my knowledge on cron is very very limited, so am really not sure if env variables are supported by cron)

INTERVAL=5 # first value
cron entry uses $INTERVAL
your script runs, at the end calls database qry to set INTERVAL to value from database.

lemme know if this works, I ll atleast know then if cron can accept environment parameters :slight_smile: