Zend Framework 1.11 and CRON jobs. Wait, wut?

Setup Zend Framework CRON jobs

Setup CRON jobs for Zend Framework applications? Yes it is possible and actually it’s pretty simple and elegant. Read below to see how to use create zend framework cron jobs.


Every serious web application demands large and/or heavy amounts of calculations to be made, or data to be prepared outside the client flow. Probably the best solution for this is to run CRON jobs. Running CRONs is sometimes pretty easy, just execute a .php file, it does what it does and that’s it, but when it comes to ZF, you may need to use more than just custom initialized PDO and files I/O. If you need to run Zend_Db, Zend_Db_Table models, Zend_Cache, or other Zend Framework libraries in your CRON, you will need to do it The Zend Way.


Just create a project provider in your project’s directory, using your ZF console tool. To do so, make sure you have a working ZF tool and run:

./bin/zf.sh create project-provider MyCron run

You can verify the results of this operation by taking a look inside your project’s “providers” directory and by running the ZF tool again to list the available providers and actions:


The next step is to open the provider file in the “providers” directory and add an init() method as a private or protected method, depending on whether you are going to extend this provider or not. Public methods are visible in ZF tool, so the init method should never be public.

     * @var Zend_Db_Adapter_Abstract
    private $db;
     * @var array 
    private $config;

    private function init()
	$this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION); //load .zfproject.xml

	$bootstrapResource = $this->_loadedProfile->search('BootstrapFile'); //get application bootstrap

	/* @var $zendApp Zend_Application */
	$zendApp = $bootstrapResource->getApplicationInstance(); //initialize application instance

	    //bootstrap resource plugins for later usage

            //new Zend_Session_Namespace(); //uncomment if you are going to use Facebook API, prevents from session_start bug
	catch (Zend_Application_Exception $e)
	    throw new Zend_Tool_Project_Provider_Exception($e->getMessage());
	$this->db = $zendApp->getBootstrap()->getResource('db')->getConnection(); //get database instance for later usage

	$this->config = $zendApp->getOptions(); //get the contents of application.ini as array

Now, you can use the init() method inside your action methods to configure the povider.

    public function run()

	//awsome stuff go here


The final step is to setup your crontab to execute the ZF console tool and your project provider at predefined time intervals or events. To do this, edit the file, opened by “crontab -e” command

* * * * * cd /path/to/project && ./bin/zf.sh run my-cron

This for example will execute the project provider’s run() method each and every minute. For more information about CRONTAB settings, see here or run “man crontab