PHP: running scheduled jobs (cron jobs)

Eikern picture Eikern · Sep 23, 2008 · Viewed 61.9k times · Source

I have a site on my webhotel I would like to run some scheduled tasks on. What methods of achieving this would you recommend?

What I’ve thought out so far is having a script included in the top of every page and then let this script check whether it’s time to run this job or not.

This is just a quick example of what I was thinking about:

if ($alreadyDone == 0 && time() > $timeToRunMaintainance) {
   runTask();
   $timeToRunMaintainance = time() + $interval;
} 

Anything else I should take into consideration or is there a better method than this?

Answer

Armin Ronacher picture Armin Ronacher · Sep 23, 2008

That's what cronjobs are made for. man crontab assuming you are running a linux server. If you don't have shell access or no way to setup cronjobs, there are free services that setup cronjobs on external servers and ping one of your URLs.