How to call a controller/action in crontab?

If you want to call a controller/action in crontab, you simply must:

1. create a copy of index.php in your app’s webroot directory,
and rename it to cron_dispatcher.php or to any filename.

2. after this, somewhere in line 78 of index.php of your app’s webroot directory,

require CORE_PATH . ‘cake’ . DS . ‘bootstrap.php’;

put code:

if($argc == 2) {
$Dispatcher= new Dispatcher();
$Dispatcher->dispatch($argv[1]);
}

this code will get argument from php commandline having index = 1.

so, in your crontab,

* * * * php -q /var/www/html/cake/webroot/cron_dispatcher.php Controller/action

6 Responses so far »

  1. 1

    Robby said,

    February 11, 2008 @ 11:37 pm

    Could you also use cake console command and put a wrapper in vendors/shells directory to acheive the same effect?

  2. 2

    phpcurious said,

    February 12, 2008 @ 3:03 am

    @Robby,

    thank you very much for visiting my site.

    I haven’t tried that yet in cakephp 1.2, since I’m mostly a cakephp 1.1 user. I think you are referring to newer features of cakephp in 1.2 version. I believe it is possible, basing from daniel hoffster’s blog post , “Writing a custom CakePHP console script”.
    I don’t want to elaborate here how he discussed to create a sample console script, but
    you can just visit his site at this address:
    http://cakebaker.42dh.com/2007/05/07/writing-a-custom-cakephp-console-script/

  3. 3

    brainv said,

    February 25, 2008 @ 4:37 am

    why not use lynx or wget command to call your script?

  4. 4

    phpcurious said,

    February 25, 2008 @ 9:08 am

    @brainv

    thanks for visiting my site.
    yes you can also do that. though, IMHO,
    that’s not a good practice in cakephp.

  5. 5

    samoli said,

    February 25, 2008 @ 7:06 pm

    @brainv

    Calling a script with lynx is OK for small tasks, but if you have a job which runs for many hours, the web server will potentially end the process. It’s usually much better to call it directly.

  6. 6

    phpcurious said,

    February 26, 2008 @ 10:58 am

    @samoli

    thanks for visiting my site.
    nicely explained.

Comment RSS · TrackBack URI

Say your words