Cron Job in CakePHP 1.2

November 12th, 2007 | by useful | in Programming |

After looking around the net for a way to execute cakephp scripts via a crontab I became fed up and made my own solution. I simply added a function to my existing controller and then executed it via a wget.

class PostsController extends AppController
function crontab {
// 404 if the request isnt from the localhost
if($_SERVER(’REMOTE_ADDR’) != ‘127.0.0.1′))
{
$this->cakeError(’error404′);
exit; // dont know if this is needed
}
// execute the cronjob
doSomething();
exit; // dont want to use a view
} // end controller

Now we just make a cron job to run every five minutes

wget –spider http://mydomain.com/posts/crontab

Tags: | |

Site RSS Feed | Comment RSS Feed | Trackback.

3 Responses to “Cron Job in CakePHP 1.2”

  1. ketan Says:

    nice post! a logical extension to this would be reporting tool showing the daily cron execution. Anyhow gr8 going.

  2. ketan Says:

    also maybe u can put that check for localhost to the beforeFilter method! this way u can add different actions in the controller (specifically for cron jobs)

  3. Guillaume Says:

    Hello !

    I think you should check Shells and Tasks.

    http://book.cakephp.org/view/110/creating-shells-tasks

Leave a Reply

required

required, private