Click to See Complete Forum and Search --> : Executing code at a specific time


kanji2150
October 9th, 2005, 01:26 PM
Hey guys, I am working on a project now where I wanted to implement automatic database maintenance. I basically need to clean old records from the system every morning at 12:01AM. Is there a way to do this reliablely with PHP or am I going to have to implement a server-based solution? Thanks for the input guys, you're always the best!

Jason Palmer
Webmaster Digital-Play Web Designs
http://webdesign.digital-play.com/

PeejAvery
October 9th, 2005, 08:45 PM
Can't be done with PHP unless you plan to set a refresh statement. Best option is to set the script to run from a task scheduling agent. Worst case scenario is using JavaScript to detect the time with a setTimeout() and tell it too look for the time and then refresh the browser accordingly.

MadHatter
October 9th, 2005, 09:07 PM
CRON (http://rootr.net/man/man/cron/8). just write a script that does the work, and schedule it w/ a cron job, & it will get done at the specified time. I've done this w/ databas back up (where it would back up the db, tarball the directory & email it to me).

kanji2150
October 9th, 2005, 10:29 PM
I knew I could count on you guys for a fast response! Thank you all for your input! Earlier today I was actually looking into using CRON however I was given the impression that the execution of the job depends solely on if people visit the site regularly. So, if I am correct in my interpretation, then if there was a day when no one visited my site from 11pm until 8am the next morning, that the scheduled job that was supposed to execute at 12:01AM would not execute until 8AM the next morning when someone finally told the server to refresh by visiting the page. Is this true? At first, this will NOT be a high traffic site.

PeejAvery
October 9th, 2005, 10:58 PM
So then run a JavaScript code that refreshes a page with PHP every time the clock hits 00:01.

<html>
<body onload="update()">

<script language="JavaScript">
function update(){
var digital = new Date();
var h = digital.getHours();
var m = digital.getMinutes();
var s = digital.getSeconds();
time = h + ":" + m + ":" + s;

if(time=="00:01:00"){window.location.reload();}

setTimeout("update()", 1000);
}
</script>

<?php

//PLACE PHP CODE TO CLEAN DATABASE HERE.

?>

</body>
</html>
***But use this as last case scenario.

bigBA
October 9th, 2005, 11:20 PM
I knew I could count on you guys for a fast response! Thank you all for your input! Earlier today I was actually looking into using CRON however I was given the impression that the execution of the job depends solely on if people visit the site regularly. So, if I am correct in my interpretation, then if there was a day when no one visited my site from 11pm until 8am the next morning, that the scheduled job that was supposed to execute at 12:01AM would not execute until 8AM the next morning when someone finally told the server to refresh by visiting the page. Is this true? At first, this will NOT be a high traffic site.

well no.
as MadHatter pointed out, you can use some CRON job to do this. and the cron job doesn't rely on the webserver. just execute the script with the command-line php interpreter - via the cron job.

the solution posted by peejavery is a little bit unsuitable for your purpose... his solution in deed depends on some people keeping open the webpage...

kanji2150
October 10th, 2005, 01:40 AM
Thanks again for all the replies guys! Ideally, I am looking for a client-side solution because I do not have direct access to the web server that our pages are hosted on. I will have to request that a CRON job be added, and that request may not be satisfied. Are there any other solutions that you guys can think of? We are also going to be developing a visual basic application that will act as the adminstrator to this software. It has a connection to our MySQL database, however the computers it will be installed on have to be turned off by 5pm every day lol. I don't know what to do.. I'm about to just dedicate one of my machines to call a script at 12:01 everyday haha!

MadHatter
October 10th, 2005, 03:02 AM
if it were me, I'd still write the script that does what you need, and set up a cron job on *my* machine that logged in and ran the script (which would archive the site and email it back to me, or whatever you're doing).

if you can connect to your database from outside the webserver, then I say just write a little app that logs into it and pulls the info out that way.