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.
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.
Done with addjob
with the help of addjob,I added to save in file in it,Nice extension.Appreciated!
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?
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.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.