Yii 1.1: yii-crontab

Manages system cron jobs. Supports Application console commands
23 followers

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.

Total 6 comments

#14387 report it
rereadyou at 2013/08/07 10:11pm
The saveToCrontab function don't seems works

Hi, there, i don't think saveToCrontab function in Crontab class on line 146 works correctly. The return value will be 0 if cmd executed successfully.

#13434 report it
Abhishek Shah at 2013/05/29 05:09am
working with windows

cron job is feature of linux,is this extension also work fine with windows?I mean,If I schedule event,with it on windows,will it work fine?

#11868 report it
Abhishek Shah at 2013/02/09 01:23am
Done with addjob

with the help of addjob,I added to save in file in it,Nice extension.Appreciated!

#11823 report it
Abhishek Shah at 2013/02/06 06:38am
Want to save in file

I want to save result of cron job in file,when I am creating cron job by this way,what to do?

#10777 report it
Anil Konsal at 2012/11/23 12:59am
Awesome! Your extension worked well.

Hi, Thanks for this great extension. I used it to make a small mailer system and it worked well. I needed to add a function removeJobById which removes a job by some job id provided in the command of cron job.

#5576 report it
Roman Solomatin at 2011/10/21 11:15am
Interesting

I need to try it out. Thanks!

Leave a comment

Please to leave your comment.

Create extension
  • Yii Version: 1.1
  • License: MIT License
  • Developed by: yiimann
  • Category: Console
  • Votes: +14 / -2
  • Downloaded: 2,059 times
  • Created on: Jul 31, 2010
  • Last updated: Nov 3, 2010