[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 626
  • Last Modified:

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?
  • 2
1 Solution
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
(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
Julian HansenCommented:
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 HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Disregard my previous comment.  For some reason I thought this was an Access question.
Ray PaseurCommented:
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.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now