3

I am using CakePHP 2.0 and have been trying to setup a cronjob to execute a method in a controller. I've been going nuts, looking over various tutorials and random sites to see if I could find a solution.

The error I am receiving is this:

Undefined variable: argc [APP/webroot/cron_dispatcher.php, line 83

Here is the bottom of the cron_dispatcher.php file in my app/webroot/ directory.

if (!include(CORE_PATH . 'cake' . DS . 'bootstrap.php')) {
        trigger_error("CakePHP core could not be found.  Check the value of CAKE_CORE_INCLUDE_PATH in APP/webroot/index.php.  It should point to the directory containing your " . DS . "cake core directory and your " . DS . "vendors root directory.", E_USER_ERROR);
    }
    if (isset($_GET['url']) && $_GET['url'] === 'favicon.ico') {
    return;
    } else {
      define('CRON_DISPATCHER',true);
    if($argc >= 2) {
        $Dispatcher= new Dispatcher();
        $Dispatcher->dispatch($argv[1]);
        }
    }

I cannot find where these variables ($argv and $argc) are defined. They aren't defined anywhere in the dispatcher.php file itself. I searched Google to no avail. I'm not 100% sure how the Dispatcher works, but any help would be greatly appreciated. Thanks.

== UPDATE GoDaddy's shared hosting doesn't allow you to change the settings of argc argv.

Brian Weinreich
  • 7,692
  • 8
  • 35
  • 59
  • possible duplicate of [How to setup cronjobs in cake php?](http://stackoverflow.com/questions/13949539/how-to-setup-cronjobs-in-cake-php) While the objective is "to execute a method in a controller" - that's _the wrong way to do it_. – AD7six Jun 11 '14 at 11:53

4 Answers4

17

In case anyone else is interested,

In the new CakePHP 2.0.5, you will an index.php in webroot folder:

Copy this file and name it cron_dispatcher.php, and place it into the same directory (webroot)

You will find this code at the very bottom:

$Dispatcher = new Dispatcher();
$Dispatcher->dispatch(new CakeRequest(), new CakeResponse(array('charset' => Configure::read('App.encoding'))));

change the bottom to

define('CRON_DISPATCHER',true);
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch(new CakeRequest($argv[1]), new CakeResponse(array('charset' => Configure::read('App.encoding'))));

You're doing two things here: Setting CRON_DISPATCHER to true, and passing environment variables ($argv[1]).

In your controller, add this line before you do anything else:

if (!defined('CRON_DISPATCHER')) { $this->redirect('/'); exit(); }

This will ensure people going to yoursite.com/controller/cronaction won't be able to run your script.

In your htaccess file in webroot, add this:

<Files  "cron_dispatcher.php">
    Order deny,allow
    Deny from all
</Files>

This will ensure poeple going to yoursite.com/cron_dispatcher.php won't be able teo run it.

Now set up the cron job using something like the command:

php /home/yoursite/public_html/cakephp/app/webroot/cron_dispatcher.php /controller/cronjobaction
Vigrond
  • 8,148
  • 4
  • 28
  • 46
  • Thanks for this, I have followed the instructions above, when the cron runs I am getting this error: Status: 302 Found X-Powered-By: PHP/5.3.8 Set-Cookie: CAKEPHP=0b8743f09a88ad2fbcf3a821355d46c7; expires=Mon, 05-Nov-2012 00:25:02 GMT; path=/; HttpOnly Location: https:///tasks/cron_emailer Content-Type: text/html; charset=UTF-8. Any idea where I might be going wrong? Thankyou – sluggerdog Nov 02 '12 at 00:31
2

There is no need for a Cron Dispatcher

There is, and has never been, a need to create a cron dispatcher. The only thing required to use a CakePHP application via cron is to create a console command, and call it:

*/5  *    *    *    *  cd /full/path/to/app && Console/cake myshell myparam

At some point in the past, there was some poor advice in the book which has since been corrected.

Community
  • 1
  • 1
AD7six
  • 63,116
  • 12
  • 91
  • 123
2

$argc and $argv is environmental variables set in CLI.
You might need to check your PHP setting on register_argc_argv (which should NOT disabled)

ajreal
  • 46,720
  • 11
  • 89
  • 119
  • According to http://docs.php.net/manual/en/features.commandline.differences.php register_argc_argv is enabled by default in the CLI and you cannot disable it via php.ini. – dhofstet Dec 13 '11 at 10:36
  • @dhofstet I think you misunderstoo.This is enabled by default.And It can be disabled in php.ini. – ajreal Dec 13 '11 at 10:41
  • Ah very interesting. Any ideas why it says that its undefined? You think I should put it in the app/ directory instead of the webroot? I feel like that wouldn't have an effect, as the $argc should be available in all files on the system, no? – Brian Weinreich Dec 13 '11 at 16:31
  • If the config has disabled then is become undefined. **OR** are you calling the $argc in an Object class? – ajreal Dec 13 '11 at 16:36
0

I'm not sure how did you set up your cron job, but how about something like this:

*/10 * * * * wget -O - -q "http://localhost/url/to/your/controller/action"

Of course, a better idea might be to create a cake shell and run that, depending on your needs.

dr Hannibal Lecter
  • 6,665
  • 4
  • 33
  • 42