Howto: Zend Framework Cron

This was verified to work under Zend Framework 1.9.5 and 1.9.6.

First we want to make a new file for the cron somewhere outside the public folder, I chose a folder called scripts.
My project thus looks like this:

  • domain.com Fresh Sweet CRON
    • application
    • library
    • logs
    • public
      • index.php
    • scripts
      • cronjob.php
    • tests

The trick with cronjobs is that you do not want to load the whole View part of ZF, we don’t need any kind of HTML output! To get this to work, I defined a new constant in the cronjob.php which I will check for in the index.php.

cronjob.php

define(“_CRONJOB_”,true);
require(‘/var/www/vhosts/domain.com/public/index.php’);
….. rest of your code goes here, you can use all Zend components now!

index.php

date_default_timezone_set(‘Europe/Amsterdam’);

// 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’) : ‘production’));

// 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();

/** Cronjobs don’t need all the extra’s **/
if(!defined(‘_CRONJOB_’) || _CRONJOB_ == false)
{
$application->bootstrap()->run();
}

It’s as easy as that!
You’d be surprised how long it took me to find the answer to this simple problem, like many other opensource projects documentation for ZF has to sometimes be found in the community and a lot of that is outdated. This was just one of a few solutions but it works just fine for me and is pretty simple to implement so I decided to stick with this.

I hope this can help someone out

Leave a comment

5 Comments.

  1. Thanks a bunch! I had previously setup a custom route to point to a cron action in the index controller, but I like this better.

    I did things a little bit differently, though.

    I called my constant “IS_SCRIPT” since I have a couple other scripts besides cron in my scripts folder.
    I changed the second to last line to “$application->run()” instead of “$application->bootstrap()->run()”.

  2. Thank you very much. This is exactly what I’m looking for.

  3. Worked beautifully. Thanks much.

  4. You’ve made mistake the code should be like this if you want to impart Zend library to the cron jobs

    /** Cronjobs don’t need all the extra’s **/
    if(!defined(‘_CRONJOB_’) || _CRONJOB_ == false)
    {
    $application->bootstrap()->run();
    }esle{
    $application->bootstrap(‘Db’);
    $application->bootstrap(‘Registry’);
    /* ..etc.. */
    }

    anyway your method is quite nice and thank you very much

  5. Sorry, it should be

    /** Cronjobs don’t need all the extra’s **/
    if(!defined(‘_CRONJOB_’) || _CRONJOB_ == false)
    {
    $application->bootstrap()->run();
    }else{
    $application->bootstrap(‘Db’);
    $application->bootstrap(‘Registry’);
    /* ..etc.. */
    }

Leave a Reply


[ Ctrl + Enter ]