codeigniter
Skapa cronjob i kodigniter på Linux-hosting-server
Sök…
Ringa en CodeIgniter-controller från cron
// application/controllers/Company_controller.php
<?php
if(!defined('BASEPATH'))
exit('No direct script access allowed');
class Company_controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('companies_model');
}
// cron entry would be something like this:
// 1 1 * * * /usr/bin/php [full path to]/index.php company_controller cronCLI AcmeCorp >/dev/null 2>&1
public function cronCLI($firmName) {
if(php_sapi_name() == 'cli') {
$this->companies_model->doSomeDB_Process($firmName);
} else {
echo 'CLI only';
}
}
}
Cronjob i kodigniter
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cron extends CI_Controller
{
/**
* This is default constructor of the class
*/
public function __construct()
{
parent::__construct();
$this->load->library('input');
$this->load->model('cron_model');
}
/**
* This function is used to update the age of users automatically
* This function is called by cron job once in a day at midnight 00:00
*/
public function updateAge()
{
// is_cli_request() is provided by default input library of codeigniter
if($this->input->is_cli_request())
{
$this->cron_model->updateAge();
}
else
{
echo "You dont have access";
}
}
}
?>
Ring detta från din cpanel / cron manager på följande sätt (jag har lagt till fler sätt att kalla det):
0 0 0 0 0 php-cli /home/your_site_user/public_html/index.php cron updateAge
ELLER
0 0 0 0 0 wget http://your_site_url/cron/updateAge
ELLER
0 0 0 0 0 /usr/bin/php /home/your_site_user/public_html/index.php cron updateAge
I mitt fall: wget-saken arbetar på plesk och cpanel (wget skapar filer på servern i din rotkatalog). php-cli fungerar både på plesk och cpanel.
Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow