CodeIgniter Cron Job through Cpanel

2020-02-13 05:44发布

问题:

Ok I looked at every other thread and have done exactly what they've done and what it says in the manual and I can NOT figure this out for the life of me. The results of the cron job are being emailed to one of my emails. ALL it is doing is printing out the html markup of the layout... And printing the base page content... It's like it's not registering anything.

php /home/jdstable/public_html/dev/index.php cron decrease_pets_stats

That's my command line.. I tried replacing php with the user/local/bin/php thing as well and it didn't work. The thing is is that I have other cron jobs running off procedural PHP code that work FINE with php path/to/cron.php... But it won't work with CI..

My controller is Cron and my method is decrease_pets_stats..

//decrease pets stats
public function decrease_pets_stats() {
$this->load->model('Cron_model', 'cron');
    $this->cron->decrease_pets_stats();
    echo 'Decreased pet stats';
}

And here is the logic of the method:

//decrease pets stats
//runs every hour
public function decrease_pets_stats() {
    $this->db->set('hunger', 'hunger - 5');
    $this->db->set('happiness', 'happiness - 5');
    $this->db->set('loyalty', 'loyalty - 5');
    $this->db->update('user_creature');
}

Does anyone have any idea why it's just printing the layout markup? My constructor looks like this:

public function __construct() {
        parent::__construct();
        if( ! $this->input->is_cli_request()) show_error('Direct access is not allowed');

        $this->load->model('Cron_model', 'cron');
    }

And my parent constructor holds quite a bit of stuff (loading helpers and libraries along with getting the user information to appear on each page if they are logged in.

Does it matter if this is at the top of the controller before even opening the controller Cron class?

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

回答1:

The default PHP install you are using was probably compiled as CGI-FCGI, not for CLI. It depends on your host and/or server, but you'll need to search for your PHP install for the command line interface, and then use that in your cron job. I had the exact same problem on Hostmonster, and my cron command ended up being:

/ramdisk/bin/php5-cli ~/public_html/sitefolder/index.php controller method

For me, the PHP I needed was in /ramdisk/bin/php5-cli.



回答2:

For CodeIgniter 2.2.0

You can try this two method:

  1. php-cli /home/username/public_html/index.php controller method
  2. wget http://www.example.com/controller/method

or at your case

  1. php-cli /home/username/public_html/index.php Cron decrease_pets_stats
  2. wget http://www.example.com/Cron/decrease_pets_stats

It works fine with me.. Cheers!



回答3:

Had the same issue and after trying whatever I could thing the obvious worked... /usr/local/bin/php /absolute/path/to/index.php cron



回答4:

/usr/local/bin/php /home/jdstable/public_html/dev/index.php cron decrease_pets_stats

This fixed it.



回答5:

Here is solution first you need to find path from phpinfo document_root file name

php5 /home/abc/public_html/index.php folder_name controller function