5
votes

How run zend framework action (inside index controller) by cron every 12 hours?

The case:

  1. I have basic(no modules) zend project (1.11) that created by zf tool.

  2. Inside main IndexController exist cronAction() - url http://mydomain/index/cron.

  3. Need to run cronAction() once per 12 hours by cron.

Thanks

3

3 Answers

13
votes

Find the crontab file and add this line:

0 0,12 * * * curl --silent --compressed http://mydomain/index/cron

You can also do it with other tools, such as lynx or wget, not necassarily curl - the above is just an example.

5
votes

I know I am bit late but I would like to leave another solution, maybe it help other people, you could run the file in cron if you have your business rule inside model

By creating a file in the public folder with the content below. Ex.: cron.php

 <?php

 // Define path to application directory
 defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

 // Define application environment
 defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));

 // Ensure library/ is on include_path
 set_include_path(implode(PATH_SEPARATOR, array(
        realpath(APPLICATION_PATH . '/../library'),
        get_include_path(),
    )));

 /** Zend_Application */
 require_once 'Zend/Application.php';

 // Create application, bootstrap, and run
 $application = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini'
 );
 $application->bootstrap();

 $model = new Application_Model_Name();
 $model->runTask();

Then add a cron tab entry

0 0,12 * * * php /path/to/your/project/cron.php

It should work better than first answer since you will run using PHP CLI then you won't have execution time limit of php script, in case of your script takes more than one minute and you don't need network connection to run that cron job

0
votes

In Zend Framework 2 You can run a cron job using console routes. Take a look at the example posted here: http://collabedit.com/58v4v