How to: Cron Jobs

It should be like this:


C:\xampp\php\php.exe console.php imageCacheCleaner

Your console.php




<?php

// change the following paths if necessary

$yii=dirname(__FILE__).'/../../framework/yii.php';

$config=dirname(__FILE__).'/protected/config/console.php';


// remove the following lines when in production mode

defined('YII_DEBUG') or define('YII_DEBUG',true);

defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);


require_once($yii);


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



and your config/console.php as is

imageCacheCleaner class must be in protected/commands

Try Image_cache_cleanerCommand as well. I’m not sure if camel case can cause problem.

Also check blob it will reduce code lines

Hi thanks for the replies so far.

Ok, it works in Windows, but not working in Linux.

I executed:


/usr/bin/php /var/www/vhosts/beta/console.php imagecachecleaner

And produced this error:

I’ve been playing around, changing this and that but nothing works, please help.

Ok, here’s a step by step, how i create such a command starting with a fresh application created with:


yiic webapp ./myapp

  1. Edit myapp/protected/config/console.php:

return array(

    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

    'name'=>'My Console Application',


    'params'=>array(

        'testparam'=>'testvalue',

    ),

);



  1. Create myapp/protected/commands/DemoCommand.php:

<?php

class DemoCommand extends CConsoleCommand {


    public function run($args)

    {

        echo "Hello! Param=".Yii::app()->params['testparam']."\n";

    }

}



  1. Check that yiic finds the command:

[mike@web myapp]$ ./protected/yiic

Yii command runner (based on Yii v1.1.3)

Usage: ./protected/yiic <command-name> [parameters...]


The following commands are available:

 - demo

 - message

 - shell

 - webapp



  1. Run the command:

[mike@web myapp]$ ./protected/yiic demo

Hello! Param=testvalue



This is how i create all my cron scripts. Very easy. Can you verify this? Note: It’s essential that you call the yiic command that is in protected.

Ok, so when I am in the [font="Courier New"]/var/www/vhosts/beta[/font] folder, I executed the command like below, it executes with no problems:


$ /usr/bin/php protected/yiic imagecachecleaner

but when I executed it from the root, using the command as below:


$ /usr/bin/php /var/www/vhosts/beta/protected/yiic imagecachecleaner

or like this:


$ /usr/bin/php /var/www/vhosts/beta/protected/yiic

, it displays this error:

Thank you for the guide, but I think we are getting closer…I hope you can help me to figure out why this happened.

Can you call yiic directly? You don’t need to pipe this through /usr/bin/php. All you have to do is set yiic executable (chmod a+x yiic).

For my example above: It works from anywhere, no matter if i’m in the directory or use the full path to yiic. Can you do some tests with the same example i posted (using a fresh clean webapp)? Just to make sure it’s your environment, not some misconfiguration.

Hi Mike,

So the system administrator at my workplace made a shell script:


#!/bin/bash

#yiic

cd /var/www/vhosts/beta

#running yiic

/usr/bin/php protected/yiic imagecachecleaner >> /home/admin/logs/imagecachecleaner.log



Now it is working…but I don’t know if this is a good solution ? hahaha…because what he actually does is recommand the script then call the crontab from the shell…

But, I will try your solution later on. Thanks a lot for this info…wil keep you updated.

Best!

You should not need all this. Like i said i use many cronjobs the way i described before. No problems at all.

Error is because the $yii and $config in the following line are not determining the absolute path.

*** change the following paths if necessary ***




$yii=dirname(__FILE__).'/../../framework/yii.php';

$config=dirname(__FILE__).'/protected/config/console.php';



You also might want to add the php and yiic location in the PATH environment variable of Windows and reboot your computer if using XAMPP… Was having similar issues but resolved it… never really had any problems until started mucking around with commands in yii…

This article is for WAMP (below is the link) but gives you the general idea on how to configure them…

Source: http://www.yiiframework.com/doc/cookbook/3/

Just in case you hadnt done this bit (like I hadn’t!)

cheers ;)

edit: Deleted (reason, duplicated post… weird was posting a reply and it appeared on a different thread. Had multiple yii forum tabs open in Firefox…)

Hi Mike/Yii community,

I followed the instructions above that Mike posted for the console app and it works like a charm. However, it doesn’t work when you have layed out the yii app directory structure differently then what’s generated with the yiic tool.

My directory structure looks like this:

approot

approot/protected

approot/runtime

approot/www <-- location of web accessible assets/js/css/themes/index.php entry script

approot/yii <-- symlink to the latest yii framework lib

I’ve created the console entry script here:

approot/protected/scripts/myscript.php

and the commands in the default loc:

approot/protected/commands/MyCommand.php

2 problems here:

1)When I run the following command to use the local yiic tool in my app nothing is executed.

> approot/protected/yiic

>> doesn’t show me the commands available

2)When i run the command from within the scripts folder like so:

> yiic myCommand

>> doesn’t output any errors or anything

Can anyone here please advise how to resolve this, as it appears to be a configuration issue? Many thanks!

There are older threads that are good resource to this need.

Use ./yiic instead of just yiic.

Otherwise you’ll be running the Yii framework yiic, not your project yiic.