run an update task without a cron job

I do not have access to cron jobs as I am on a shared server. I would still like to run an update task on my database at set intervals. How can I best achieve this. Obviously I could check the time in which index.php is executed and if it is on the hour I could include my update script. But then there is a risk of the script not executing (no users request a page at that second) or many executions.

Any ideas?

Answers


Are you sure you don't have access to cron?

crontab -e

You just might...

Edit: Also, Please don't attach an update script to index.php at one poor user's expense every hour...


Run a script on a server that does have cron to access a url that runs the php script on the hour. I use wget in my crontab to do that sort of thing.


You're part of the way there. Don't check if the time is "on the hour"; check the difference between the current time and the time of the last update. This, of course, requires you to store a timestamp somewhere when you run the update.

The biggest problem with this approach is that the page won't load until the update is complete. Depending on how long the update takes, this might not be a factor; but if it is, you might want to spawn a new thread for the update (I'm not even sure if that's possible in PHP).


You guys are close. Rather than put the periodic functions in the actual PHP of the page the user is requesting, have a javascript request happen from the user's page. This way, you can be certain that the page load will not be slowed down, and the javascript will fire onLoad(). Keep the stored 'last processed' value as suggested above, and only run the cron function once in a given interval.

Here's the JS to use to call something remotely. It will only work in modern browsers with JS enabled of course...

<script>
        function DoCron()
        {
            var xhr; 
            try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
            catch (e) 
            {
                try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
                catch (e2) 
                {
                  try {  xhr = new XMLHttpRequest();     }
                  catch (e3) {  xhr = false;   }
                }
             }

            xhr.onreadystatechange  = function()
            { 
                 if(xhr.readyState  == 4)
                 {
                      if(xhr.status  == 200) 
                      {
                            alert("Cron Worked");
                      }
                      else 
                      {
                         return 'ERROR';
                      }
                 }
            };  

            xhr.open("POST", "CRON.php", true); 
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
            xhr.send('');
        }

    </script>

Need Your Help

Cyclic serialisation with Many to Many relationship with Hibernate

java hibernate orm jpa

I have a parent (Program) pojo with a many-to-many relationship with their children (Subscriber).