Home > Software design >  CI4 Cron Job in CPanel for installation without index.php
CI4 Cron Job in CPanel for installation without index.php

Time:10-14

I have a Cron Controller that I would like to run from CPanel's cron job functionality, however my installation does not have an index.php file as I have used mod_rewrite on my .htaccess file so it doesn't show in my url.

I've read the documentation on running it through cli and can only get an input in the error_log using /usr/local/bin/php /home/user/subdomain.domain.com/app/Controllers/Cron.php

I'm getting the following error:

[05-Oct-2022 21:36:01 UTC] PHP Fatal error: Uncaught Error: Class 'CodeIgniter\Controller' not found in /home/user/subdomain.domain.com/app/Controllers/Cron.php:7

Stack trace: #0 {main} thrown in /home/user/subdomain.domain.com/app/Controllers/Cron.php on line 7

This is how my Controller is setup

<?php

namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\CronModel;

class Cron extends Controller
{
    public function __construct(){
        $db = db_connect();
        $this->cronModel = new CronModel($db);
    }
    
    /* 
     * Function to start cronjob if it's time
     */
    public function index(){
       $this->cronModel->run();
    }

}

No other cli reaches the controller and I keep getting this error.

EDIT: I have this in my Routes file: $routes->cli('cron/index/(:segment)', 'Cron::index/$1');

CodePudding user response:

I've managed to get it to work. There was a rewrite on the .htaccess file on Codeigniter's root. So basically I added the rewrite rule to the cli path and it worked, like so:

/usr/local/bin/php /home/user/subdomain.domain.com/rewrite_rule_path cron run
  • Related