I want to set cron job using codeigniter, in godaddy shared hosting i used the following code to execute movies controller's cool method.

/web/cgi-bin/php5 "$HOME/html/MOVIESDOM.COM/index.php" movies cool

Problem is it always execute default controller.

Pls tell me how to execute movies controller's cool method

Recommended Answers

you need to create routing directives for it.

$route['movies'] = "movies/cool";

assuming that you have movies.php in your application/controllers/ directory and it is coded something similar to this

class Movies extends CI_Controller
{

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

    public function index()
    { …
Jump to Post

The dude can also test it on his CLI like so,

php /the_main_cron_class.php movies/cool
Jump to Post

All 7 Replies

you need to create routing directives for it.

$route['movies'] = "movies/cool";

assuming that you have movies.php in your application/controllers/ directory and it is coded something similar to this

class Movies extends CI_Controller
{

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

    public function index()
    {

        // you can do some other options as opposed to just cool method //

    }

    /* 
    * @cool method
    * execute cron job
    */

    public function cool()
    {

        //put your cron here
        // don't forget to run it on the background

        //catch cron-job log here if needed
        //if cron-job is successful return something or return true.

    }


    public function cron_status()
    {

        //this will check if the cron job log file exists
        // check the cron job status 
        // if a success return something 

        //call this method in the index method above, but make sure the reporting is only available to the administrator of the site.
      }

 //end of the class

 }

Another feasible option is to create a cron-job libraries and just load it whenever it is needed.

The dude can also test it on his CLI like so,

php /the_main_cron_class.php movies/cool

@veedeoo Is my command correct

@lorenzoDAlipio after setting up route config, it still executing home page

Hola Luis, gracias pero la próxima vez, por favor, en Inglés.

After further researching I was able to determine that there are two modes to php running: cgi or cli. Upon testing with php -v in shell I confirmed it was configured for cli, but running the same with a Cron job I saw the result being php running as cgi.

To resolve this I reconfigured my Cron job as follows:

/usr/local/bin/php /home/overstoc/www/admin/index.php cron update
I assume the default php that the Cron job is calling has a configuration for cgi where the server share is cli.

commented: YES, this resolves my issue. Thank you for pointing this out. :D +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.