Reputation: 567
I'm trying to set up a cron to run a codeigniter controller/method.
I have my application and system file etc outside of the root.
I have created a cli.php file as a hack to get it working, as suggested by someone on the codeigniter forums. the content of the file are:
if (isset($_SERVER['REMOTE_ADDR'])) {
die('Command Line Only!');
}
set_time_limit(0);
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'] = $argv[1];
require dirname(__FILE__) . '/public_html/index.php';
Now, I can successfully run the controller/method via cli using this termical command:
php cli.php cron/reccuring_checks
But can I get it to run as a cron?? oh dear NO i cannot, been at this for a whole day.
I've tried: (as well as 1000 different other combinations)
php cli.php cron reccuring_checks
/usr/bin/php -q /home/DOMAIN/cli.php cron/reccuring_checks
/usr/bin/php -q /home/DOMAIN/cli.php cron reccuring_checks
I get the following error
<p>Severity: Notice</p>
<p>Message: Undefined index: REMOTE_ADDR</p>
<p>Filename: drivers/Session_files_driver.php</p>
<p>Line Number: 130</p>
And a lot of the posts I can find online about this generally relate to CI2 and the method of replacing:
$_SERVER['REMOTE_ADDR'] with $this->server('remote_addr')
In the system/core/input file, but that is already amended in CI3
So has anyone got a clue how I can get this to work?
I assume the issue is that I have the core files above root, but thats what CI suggest you do for security, and I'm way to far down the line to change it now?
Upvotes: 0
Views: 4689
Reputation: 51
change this:
if (isset($_SERVER['REMOTE_ADDR'])) {
die('Command Line Only!');
}
with:
if(!$this->input->is_cli_request()){
die('Command Line Only!')
}
You shoud check codeigniter documentation
input class
https://www.codeigniter.com/user_guide/libraries/input.html
Upvotes: 1
Reputation: 2729
You don't need to create a special file nor you need any kind of hacks to get it working.
Simply, You create a controller & request it through index.php
Your cron command should look like this
/usr/bin/php -q /path/to/codeigniter/index.php controller_name method
assuming your controller is named 'cron' & your method is 'reccuring_checks' it would be something like this
/usr/bin/php -q /path/to/codeigniter/index.php cron reccuring_checks
Limiting your controller to command line access only is as simple as follows:
class Cron extends CI_Controller {
public function __construct () {
parent::__construct();
// Limit it to CLI requests only using built-in CodeIgniter function
if ( ! is_cli() ) exit('Only CLI access allowed');
}
}
If your hosting provider is using cPanel control panel, Your PHP path should be as follows:
/usr/local/bin/php
Upvotes: 1