yii-crontab Manages system cron jobs. Supports Application console commands

  1. Documentation
  2. Change Log

yii-Crontab is an extension that helps to add system cron jobs. It also support Application console commands.

Jobs can be loaded from crontab file for modification etc..

Resources

Documentation

Requirements
  • Yii 1.0 or above
Installation
  • Extract the release file under protected/extensions

In you config/main.php :

'import'=>array(
		'application.models.*',
		'application.components.*',
		'application.extensions.crontab.*',
	),
Usage

See the following code example:

// Usage with ApplicationConsole and normal jobs

	$cron = new Crontab('my_crontab'); // my_crontab file will store all added jobs
	
	$jobs_obj = $cron->getJobs(); // previous jobs saved in my_crontab
	
	foreach($jobs_obj as $job)
		echo $job->getCommand();
	
	$cron->eraseJobs(); // erase all previous jobs in my_crontab
	
	// Application console job
	$cron->addApplicationJob('yiicmd', 'test1', array(), '*');
	
	// to change job values:
	$jobs_obj = $cron->getJobs();
	$jobs_obj[0]->setParams(array("'datetime'"));
	$jobs_obj[0]->setCommandName('test');
	
	// <= adds a job with: * * * * * php /home/user/my_project/www/yiicmd.php test 'datetime'
	
	// add an other job
	$job = new CronApplicationJob('yiicmd', 'test', array("'datetime"), '0', '0'); // run every day
	$job->setParams(array("'date'"));
	$cron->add($job);
	
	// <= adds a second job with: 0 0 * * * php /home/user/my_project/www/yiicmd.php test 'date'
	
	// add a regular cron job
	$cron->addJob('/home/user/myprogram.bin', '0', '0', '*', '*', '1'); // run every monday
	$jobs_obj = $cron->getJobs();
	echo $jobs_obj[2]->getCommand();
	
	// <= adds a third job with: 0 0 * * 1 /home/user/myprogram.bin 
	
	$cron->removeJob(2); // removes job with offset 2 (last added here)
	
	$cron->saveCronFile(); // save to my_crontab cronfile
	
	$cron->saveToCrontab(); // adds all my_crontab jobs to system (replacing previous my_crontab jobs)
	

Content of /protected/extensions/crontab/crontabs/my_crontab :
* * * * * php /home/user/my_project/www/yiicmd.php test 'datetime'
0 0 * * * php /home/user/my_project/www/yiicmd.php test 'date'
// commands/TestCommand.php
<?php 

class TestCommand extends CConsoleCommand
{
    public function run($args)
    {
    	$table1=new table1;
    	
    	if($args[0] == 'date')
			$table1->date=date('Y-m-d');
    	elseif($args[0] == 'datetime')
			$table1->date=date('Y-m-d H:i:s');
    	
    	$table1->save();
    }
}

// yiicmd.php
<?php

// change the following paths if necessary
$yii=dirname(__FILE__).'/../yii.php';
$config=dirname(__FILE__).'/protected/config/console.php';

// remove the following line when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);

require_once($yii);
Yii::createConsoleApplication($config)->run();


<?php
// protected/config/console.php
// This is the configuration for yiic console application.
// Any writable CConsoleApplication properties can be configured here.
return array(
	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
	'name'=>'My Console Application',

	'import'=>array(
		'application.models.*',
	),

	// application components
	'components'=>array(
		'db'=>array(
	         'class'=>'CDbConnection',
	         'charset' => 'utf8',
	         'connectionString'=>'mysql:host=localhost;dbname=mydb',
    		 'username'=>'root',
    		 'password'=>''
		),
                // usefull for generating links in email etc...
		'urlManager'=>array(
			'urlFormat'=>'path',
			'showScriptName' => FALSE,
			'rules'=>array(),
		),			
	),
);

Change Log

August 1st, 2010
  • version 0.3
  • version 0.2.1
  • version 0.2
  • version 0.1.2
  • version 0.1.1
July 31, 2010
  • Initial release.
14 0
23 followers
2 560 downloads
Yii Version: 1.1
License: MIT
Category: Console
Tags:
Developed by: yiimann
Created on: Jul 31, 2010
Last updated: a year ago

Downloads

show all