FilChi Weblog

How to call a controller/action in crontab?

Posted on: January 8, 2008


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

Advertisements

9 Responses to "How to call a controller/action in crontab?"

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

@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/

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

@brainv

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

@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.

@samoli

thanks for visiting my site.
nicely explained.

this was a year ago hahaha!

anyway,

@samoli, in that case, your code design need to review since it’s running to long for a web application.

my intension to use wget/lynx to call the url of your script is to simplify the task w/o configuring anything.

this was a year ago hahaha!

anyway,

@samoli, in that case, your code design need to review since it’s running too long for a web application.

my intension to use wget/lynx to call the url of your script is to simplify the task w/o configuring anything.

Nice article it works for me. thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: