CodeIgniter Forums
Cron job not execute method - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Cron job not execute method (/showthread.php?tid=55493)



Cron job not execute method - El Forum - 10-29-2012

[eluser]Peri[/eluser]
Hi,
I'm trying to run a method by cron job, follow command:

Code:
php $HOME/public_html/environments/dev/meetfun/index.php rotinas deletephototemp

I received an email with the result of running and it presented the following problems:
Quote:<b>Notice</b>: Undefined index: HTTP_HOST...
<b>Notice</b>: Undefined index: SCRIPT_NAME ...
<p>Message: session_start()...

I tried to fix by changing the config.php to:
Code:
if (defined('STDIN')){
   // You should hardcode the base url for cli, otherwise it will fails.
   $config['base_url'] = "http://dev.ejsystem.com.br/meetfun/";
}else{
$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = "$root";
}

But not work.
Beyond the errosr messages reported, it also brought me the default page and not the controller and methods passed, as if I had just run php $HOME/public_html/environments/dev/meetfun/index.php.

best regards.



Cron job not execute method - El Forum - 10-29-2012

[eluser]CroNiX[/eluser]
The session class relies on those $_SERVER variables being present and set as they are part of how CI tracks the sessions, which they won't be set in a CLI request since it didn't come from the web.

So you need to manually set $_SERVER['HTTP_HOST'] as well as SCRIPT_NAME (and if your custom code uses other hard-coded settings), preferably in your index.php if it's a CLI request so the rest of the app will work. It's not enough to set the $config values.


Cron job not execute method - El Forum - 10-29-2012

[eluser]Peri[/eluser]
CroNiX thanks for the reply, I changed the variables $_SERVER and the errors stoped, but I have is that the controller $route['default_controller'], not that I passed: "routines / deletephototemp."
Code:
php $ HOME / public_html / environments / dev / meetfun / index.php routines deletephototemp

That could be a problem in the version of php, server or version of CI?
CI Version: 2.1.2
PHP Version: 5.2.9


Cron job not execute method - El Forum - 10-29-2012

[eluser]CroNiX[/eluser]
IIRC, you might need to play with the $config['uri_protocol'] setting in config to get cron jobs working. I don't use the built in CI way for cron jobs so I'm not really sure.

I use a variation of this: https://github.com/EllisLab/CodeIgniter/wiki/Cron-job-bootstrapper


Cron job not execute method - El Forum - 10-30-2012

[eluser]Peri[/eluser]
Thanks a lot CroNiX,
I did work after see the "Cron Job bootstrapper" and the post blog http://phpstarter.net/2008/12/run-codeigniter-from-the-command-line-ssh/
Was perfect !