简体   繁体   中英

500 Internal Server Error Cron Job with Codeigniter

I'm working on a cron job that adds a value to a database. I'm using Codeigniter. I'm testing the script using the terminal. It works while on my local machine (I'm using MAMP), but when I upload it to my server and run the script it give me the following error: Status: 500 Internal Server Error Content-type: text/html; charset=UTF-8

No matter what I do, it doesn't do anything different. What causes this and how can I fix it?

EDITED:

I'm using Codeigniter 3.0.3.

Cron Controller

class Cron extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('cron_m');
    }

    public function index()
    {
        if(!$this->input->is_cli_request())
        {
            echo "This script can only be accessed via the command line" . PHP_EOL;
            return;
        }

        $data = array('value' => 'test');
        $this->cron_m->save($data);
        echo 'saved!';

    }

}

Frontend_Controller

class Frontend_Controller extends MY_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper('form');
        $this->load->library('form_validation');        
     }

    public function lock()
    {
        $this->load->library('session');        
        $this->load->model('user_m');   

        // Login check
        $exception_uris = array(
            'admin/user/login',
            'admin/user/logout'
        );

        if (in_array(uri_string(), $exception_uris) == FALSE)
        {
            if ($this->user_m->loggedin() == FALSE)
            {
                $uri = $this->uri->uri_string();
                $this->session->set_tempdata('uri', $uri);

                redirect('admin/user/login');
            }
        }
    }

}

MY_Controller

class MY_Controller extends CI_Controller {

    public $data = array();

    function __construct() {

        parent::__construct();

        $this->data['errors'] = array();

    }

}

Cron_m Model

class Cron_m extends MY_Model {

    public $_table_name = 'CRON';
    protected $_order_by = 'id';

    public function __construct()
    {
        parent::__construct();
    }

}

MY_Model

class MY_Model extends CI_Model {

    public $_table_name = '';
    protected $_primary_key = 'id';
    protected $_primary_filter = 'intval';
    protected $_order_by = 'order';

    function __construct()
    {
        parent::__construct();
    }

    public function save($data, $id = NULL)
    {

        //INSERT
        if ($id === NULL)
        {           
            !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
            $this->db->set($data);
            $this->db->insert($this->_table_name);
            $id = $this->db->insert_id();
        }

        //UPDATE
        else
        {
            $filter = $this->_primary_filter;
            $id = $filter($id);
            $this->db->set($data);
            $this->db->where($this->_primary_key, $id);
            $this->db->update($this->_table_name);
        }

        return $id;

    }

}

.htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|assets|uploads|podcast\.xml)
RewriteRule ^(.*)$ /index.php/$1 [L]

Firstly write a codeigniter function in a controller. Make sure it satisfies following criteria

  • To access the codeigniter function no login should be there
  • In function you should not use any session.

Then in cpanel of your server, you can set cron based on your requirement, either every minute, every hour, etc.

Also use wget ,

wget -T 0 -O path_to_a_text_file_where_cron_will_write_errors path_to_your_script_that_should_be_run_by_cron

Hope it helps.

I figured it out. I was using just the php command, but after searching I found this post - CodeIgniter Cron Job through Cpanel . Instead of using php I'm now using php5-cli and it works like a champ.

php5-cli path/to/folder/index cron set_value

我认为cron作业只能与核心php代码一起使用,您必须制作新文件(如cron.php)并将此文件放在根目录中,并以核心php格式编写代码,然后将cron作业网址保存在cpanel上,我认为它将起作用ci的100%cron工作无法正常工作,因为我已经遇到了这个问题,请尝试这种方式

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM