tags:

views:

465

answers:

6

I'm pretty sure I've seen this done in a php script once, although I cant find the script. It was some script that would automatically check for updates to that script, and then replace itself if there was an update.

I don't actually need all that, I just want to be able to make my PHP script automatically run every 30 minutes to an hour, but I'd like to do it without cronjobs, if its possible.

Any suggestions? Or is it even possible?

EDIT: After reading through a possible duplicate that RC linked to, I'd like to clarify.

I'd like to do this completely without using resources outside of the PHP script. AKA no outside cronjobs that send a GET request. I'd also like to do it without keeping the script running constantly and sleeping for 30 minutes

+1  A: 

You may want to use the PHP's sleep function with specified time to run your code with that interval or you may want to try some online cron job services if you wish.

Sarfraz
+2  A: 

If you get enough hits this will work...

Store a last update time somewhere(file, db, etc...). In a file that gets enough hits add a code that checks if the last update time was more xx minutes ago. If it was then run the script.

Galen
Hm not a bad idea. But the script in question takes about 5-10 seconds to complete, which is way too long for a user to have to wait.
Rob
The user doesn't have to wait. There are a number of ways around that. Google asynchronous php request
Galen
A: 

Without keeping the script running constantly, you'll either have to use something hackish that's not guaranteed to actually run (using regular user pages accesses to run a side routine to see if X amount of time has passed since last run of the script and if so, run it again), or use an external service like cron. There's no way for a regular PHP script to just magically invoke itself.

Amber
but what if we apply magic to it?
Carson Myers
+1  A: 

You can either use AJAX calls from your real visitors to run scheduled jobs in the background (google for "poor man's cron", there are a number of implementations out there) or use some external cron-like service (for example a cronjob on some other machine). In theory you could just run a PHP script with no timeout and make it loop forever and fire off requests at the appropriate time, but the only thing that would achieve is reinventing cron in a very ineffective and fragile way (if the script dies for some reason, it will never start again on its own, while cron would just call it again).

Tgr
A: 

If the host includes a mysql 5.1+ db then perhaps timed triggers are availible to call the script? I like these mission impossible type questions, but need more information on what kind of playground and rules for the best answer.

RandyMorris
A: 

Either way, you will need to set proper execution time so the script does not exceed it.

Mikulas Dite