Codeigniter 2.0 CRON (boot strap | loader | CLI)

Tags: codeigniter, ci, 2.0, boot strap, loader, cron script, CRON

A simple, light-weight Codeigniter 2.0 CLI / CRON / Boot-strap loader script. Access your controllers from the command-line, helping you to effectively utilize your models / libraries and other code efficiently.

This package is based loosely on the codeigniter CLI script written by Jonathon Hill. It has been greatly modified, modularized & updated to support multi Web site implementations, more CRON specific tasks, and of course, it has been tailored to run with CI 2.0.

We elected to utilize the MVC style of CI to better suit our needs and so we use a "bootstrap" style PHP script to start our CI environment.  We have stripped the script down to where it is generic enough to support any web site you might need it for. 

If you want the script to support multiple web sites, wish to only maintain one copy of the script and are not using symbolic links to accomplish this - you'll need to modify the script slightly to capture an additional parameter VIA command-line: the application folder name. It is a simple modification, but if you are having a problem accomplishing it, please feel free to contact us for help.

Download

MD5: 23f17772dcebd5e9196b2b6833f8ef03 | SHA1: 94ba1ef1627f5056e04242db931b3b00a4197faa

Installation

  1. Download the compressed package above, which contains the cron script, the config file & the example controller and save it anywhere on your server.
  2. Unpack the package and merge the files into your desired CI installation.
  3. Modify the cron.php file in the application/config folder to match your site installation.
  4. Ensure the the cron.php file in the system/cron folder has execution permissions with the command: $> chmod +x cron.php
  5. Run the cron script from command line with the command: $> php cron.php

* Note: if you plan to use more than one controller you can remove the config over-ride array and then supply the controller and method VIA command line with the command: $> php cron.php controller/method

Source

system/cron/cron.php

#!/usr/bin/php
     // php-tag-open (we ommited it for secuirty purposes)
     // Run: php cron.php controller/method/
     
     // Block non-CLI calls
     
define('CRON'TRUE);
 
     
$path = isset( $argv[0] ) ? str_replace'cron.php'''$argv[0] ) : './';
 
     
// Load CRON config
     
require($path.'application/config/cron.php');
 
     
// Set CRON mode ( live or beta )
     
define('CRON_BETA_MODE'$config['CRON_BETA_MODE'] );
 
     
// Set index.php location
     
if( isset( $config['CRON_CI_INDEX'] ) && $config['CRON_CI_INDEX'] )
         
define('CRON_CI_INDEX'$config['CRON_CI_INDEX']);
     else
         
define('CRON_CI_INDEX'$path.'index.php');
     
     if( 
count$argv ) < )
         if( 
count$config['argv'] ) ) {
             
$argv                           array_merge$argv$config['argv'] );
             
$_SERVER['argv']    = $argv;
         } else
             die(
'Use: php cron.php controller/method');
     
     
// Simulate an HTTP request
     
$_SERVER['PATH_INFO']        = $argv[1];
     
$_SERVER['REQUEST_URI']    = $argv[1];
     
$_SERVER['SERVER_NAME']    = $config['SERVER_NAME'];
     
     
// Set run time limit
     
set_time_limit$config['CRON_TIME_LIMIT'] );
 
     
// Run CI and capture the output
     
ob_start();
 
     
chdirdirnameCRON_CI_INDEX ) );
     require( 
CRON_CI_INDEX );                                            // main CI index.php file
     
$output ob_get_contents();
     
     if(
CRON_FLUSH_BUFFERS === TRUE)
         while( @
ob_end_flush() );                                            // display buffer contents
     
else
         
ob_end_clean();
 
     echo 
"\n";
 
?> 

 

application/config/cron.php

// php-tag-open (we ommited it for secuirty purposes)
 
if ( ! defined('CRON')) exit('CLI script access allowed only');
 
 
/*
 |--------------------------------------------------------------------------
 | CRON Configuration
 |--------------------------------------------------------------------------
 */
 
 
$config['SERVER_NAME']         = 'www.example.com';
 
$config['CRON_TIME_LIMIT']   = 0;
 
$config['argv']                  = array(
                                 
1        => 'run_cron/all'
                             
);
 
$config['CRON_BETA_MODE']    = false

 

application/controllers/run_cron.php

// php-tag-open (we ommited it for secuirty purposes)
 
 /**
  * @cont:        Run_CRON
  *
  * @desc:       non-web controller
  *
  * @site:         www.example.com
  * @author:    Michael Pope
  *                      nyndesigns.com
  *
  * @file:          run_cron.php
  * @date:       10.03.2011 - 11:53:06  [Michael Pope]
  */
 
 
 
class Run_CRON extends CI_Controller {
 
     function 
__construct()
     {
         
parent::__construct();
        
         if(    ! 
defined'CRON' ) )  
             exit();
        
         
$this->load->model('Default/cron');
     }
 
 
     function 
__destruct()
     {
     }
        
     
     
/**
      * Index
      *
      * @desc not used
      */
     
function index()
     {    
     }
     
     
     
/**
      * All
      */
     
function all()
     {
         
$this->generate_sitemap();
     }
     
 
     
/**
      * Generate Sitemap XML
      */        
     
function generate_sitemap()
     {    
         
// Live Mode:
         
if( ! CRON_BETA_MODE )
             
$cron_id $this->cron->create('Sitemap (Google|Bing|Ask|Yahoo!)');
        
         
// Example Code
        
         
$this->load->library('sitemap');
         
$this->load->config('sitemap');
 
         
// ...
         // ...
         // ...
 
         // Sandbox Mode:
         
if( CRON_BETA_MODE )
             
$this->sitemap->generate_xml(nullfalse);
            
         
// Live Mode:
         
else {
             echo 
'live';
        
             
$this->sitemap->generate_xml();
             
$this->cron->update$cron_id );
         }
        
     }
 }
 
/* End of file run_cron.php */
 /* Location: ./application/controllers/run_cron.php */ 

Leave a Comment

Web Developmenthttps://www.nyndesigns.com/img/default/article/home_page_img.jpgA simple, light-weight Codeigniter 2.0 CLI / CRON / Boot-strap loader script. Access your controllers from the command-line, helping you to effectively…Codeigniter 2.0 CRON (boot strap | loader | CLI)trueWeb Design, Development, Programming and Hostingen