S
S
Sergey Okhonko2016-02-17 19:28:59
Yii
Sergey Okhonko, 2016-02-17 19:28:59

How to set up cron in yii application?

Run a cron job on a virtual host

<?php
$_SERVER["argv"][0]='Cron';
$_SERVER["argv"][1]='Cron';

if (!ini_get('register_argc_argv')) {
    if(ini_set('register_argc_argv', '1')){
        echo "Установилось \n";
    } else {
        echo "Не удалось установить \n";
    }
}
// change the following paths if necessary
$yiic=dirname(__FILE__).'/../../yii-1.1.16.bca042/framework/yiic.php';
$config=dirname(__FILE__).'/config/console.php';
$_SERVER['SCRIPT_FILENAME'] ='__DIR__';
require_once($yiic);
Yii::createConsoleApplication($config)->run();
?>

class CronCommand extends CConsoleCommand
{
    public function run($args)
    {
        $criteria=new CDbCriteria;
        $criteria->compare('id_status',1);
        $criteria->addCondition('date_started <=NOW()');
        $started=MusicTest::model()->findall($criteria);
        foreach($started as $model){
            $model->id_status=2;
        }
        $criteria=new CDbCriteria;
        $criteria->compare('id_status',2);
        $criteria->addBetweenCondition('date_finished','0000-00-00 00:00:01',date(" Y-m-d H:i:s") );
        $finish=MusicTest::model()->findall($criteria);
        foreach($finish as $model){
            $model->id_status=3;
            $model->save();
        }
    }

}

Throws an error indefinitely $_SERVER['SCRIPT_NAME']
The script itself is executed and changes to the database are made, but the actions in beforesave(), where the actions from the components are called, are not performed. How to solve this problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
matperez, 2016-02-18
@matperez

Why do you need all this pandemonium?

$_SERVER["argv"][0]='Cron';
$_SERVER["argv"][1]='Cron';

You just need to go to the folder with your yiic, which you say works via ssh, and run it.
For example:
50 3 * * * cd /usr/local/www/site.com/ && php ./yiic cron

D
Dmitry Entelis, 2014-11-01
@uranus235

1. To check if there is something in a substring, it's better to use php.net/manual/ru/function.mb-strpos.php
etc. In general, if the task is "live", then it's better not to do it ever.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question