We help IT Professionals succeed at work.

Query on timer

If I have a script that runs a query and the query comes up empty, is there anyway to have the script automatically run again in a minute or two?  Like a sleep for a few minutes mode then run again?  Without using a CRON Job?
Comment
Watch Question

Jim HornSQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015

Commented:
(1)  Set the form's Timer interval to # of seconds you want it to fire * 1000 (e.g. 3000 = 3 seconds)

(2)  Set TimerOn property to True

(3)  Write VGA code in the form's OnTimer event to run your query
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
You are talking about a script running based on a request from a browser?

If so then not really because the script runs, terminates and won't run again until it is triggered by a request to the server.

You can probably rig something up to make the script pause by running a loop

$x = time();
while(time() - $x < 60) {
 set_time_limit(60); // so script does not time out
}

But then browser will sit in a loading state until the above returns again.

The other option (not sure if this will work for you) is to send the page back with a meta refresh or javascript refresh to get the page to call the script again.

Alternatively use a javascript timer and call an AJAX function to re-run the script.
Jim HornSQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015

Commented:
Disregard my previous comment.  For some reason I thought this was an Access question.
Most Valuable Expert 2011
Top Expert 2016
Commented:
Why not just use CRON?  Anything else would feel like a jury-rigged solution.

But that said, here is a design pattern that will work (at least until something in one of the scripts fails).

1. Write the script that runs the query.  Its browser output should be the number of seconds to wait before it is to be run again.

2. Write the initiator script.  It starts the query script using CURL or file_get_contents().  With the response value, simply sleep() for that number of seconds, then loop back to start the query again.

You will probably want to add set_time_limit() into the initiator loop to make it a truly long-running job.  You will also want to have some good error checking and logging in the query script, so that if it fails for any reason, the initiator script will get the response back and can notify you via text or email that there is trouble.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.