Thursday, 2 February 2012

Setting up Cronjobs for Zend Framework Environment

For Running through SSH and Cpanel


If you need to start php cronjobs setting up your whole Zend Framework environment for use is the cronjob scripts is really easy. Just create a new directory cron in your Zend framework project. Create the directory at the same level as the application and public folder. This way the code is not accessible for the web server.

In the cron directory create a init.php file:


$time = microtime(true);
$memory = memory_get_usage();

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

// Define application environment
defined('APPLICATION_ENV')
|| define('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();

register_shutdown_function('__shutdown');

function __shutdown() {
global $time, $memory;
$endTime = microtime(true);
$endMemory = memory_get_usage();

echo '
Time [' . ($endTime - $time) . '] Memory [' . number_format(( $endMemory - $memory) / 1024) . 'Kb]';
}


This will bootstrap the application but not run it. This will start the application in the development environment (APPLICATION_ENV). You can make a cronjob environment in your application.ini if you need special settings. I added some extra information about execution time and memory usage, which might come in handy.

Now you are ready to add the real script ranking.php (my script file Name). Just add the scripts file in the cron directory and include the init.php file:



require_once 'init.php';

// The actual script.

Now you can access all namespaced zend framework classes from your project in your scripts.

Now you can run it through SSH or set the crontab in Cpanel
You can run script directly through ssh by going to the root folder of the Zend Framework

$ cd cron/
$ php ranking.php


You can review the original post at : http://www.god-object.com/2010/03/26/bootstrap-zend-framework-for-use-in-cronjobs/comment-page-1/#comment-5581
Note: Please note that this will not run through web browser. For this you can move to next section.

Running through browser


Many times developer need to test there script before final implementation and they hadn't access to SSH. Then option left to test their scripts in the browser first.
Steps to follow:
1. create new folder say cronjobs in the public folder.
2. create the script file e.g ranking.php ( in my case) as below:

error_reporting(E_ALL ^ E_NOTICE);
define("DONT_RUN_APP",true);
require('../index.php');

//Your Cron script code
/*
$model = new Application_Model_User();
$maxUser= $model->rowCount();
echo $maxUser;
*/



3. You need some modification in your public/index.php. Open it.
Replace $application->bootstrap()->run(); with below code;

if(!defined('DONT_RUN_APP') || DONT_RUN_APP == false)
$application->bootstrap()->run();
else
$application->bootstrap();



4. And that all now you can run the script ranking.php (in my case) in browser as:
http://yourdomain.com/cronjobs/ranking.php

8 comments :

  1. Great website! I am loving it!! Will be back later to read some more. I am taking your feeds also

    ReplyDelete
  2. Discovered your post really remarkable certainly. I really really enjoyed reading through it and you make rather some superior factors. I will bookmark this web site with the foreseeable future! Relly fantastic post.

    ReplyDelete
  3. I see something truly interesting about your web site so I saved to favorites .

    ReplyDelete
  4. i wish i could write so good like you do in your posts.http://www.kitsucesso.com

    ReplyDelete
  5. Debashis Chowdhury22 June 2012 at 05:42

    Hi,
    I have created a script following the steps described above. But, its not executing in live server and i am stuck. Not getting any error as well.

    My site is hosted in 1and1 server and set the cron task using ssh .

    ReplyDelete
  6. Must check the cron script by running through ssh before letting it to run it by cron.
    For this go to the SSH terminal, login in with ur User and password and run
    > php phpscriptname.php
    suppose your script in the phpscriptname.php. Once it goes fine and works your propose than go to cron tab to set this script in a cron job.
    if it works fine through ssh and not through cron than there is issue with your cron setting.
    Try! and feel free to reply your issue again

    ReplyDelete
  7. Thanks a lot mate! U rock.

    ReplyDelete
  8. How does this work with the Zend Framework 2? Would be nice if you could write an article on this subject.

    ReplyDelete