speck1990
speck1990

Reputation: 127

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]

Upvotes: 0

Views: 1431

Answers (3)

speck1990
speck1990

Reputation: 127

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

Upvotes: 1

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

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.

Upvotes: 1

Sorav Garg
Sorav Garg

Reputation: 1067

i think cron job will work only with core php code you have to make new file like cron.php and put this file in root directory and write your code in core php format and then save your cron job url on cpanel i think it will work 100 % cron job with ci doesn`t work because i have already face this issue please try this way

Upvotes: 0

Related Questions