2

I'm using CodeIgniter for my project. I have this roadblock in from of me..

There's this method in my controller that I want to call every hour. Let's have that as:

class Notifs extends CI_Controller {

    public function __construct() {
        parent:: __construct();
        // load stuff here
    }

    public function index() {

    }

    /* I want to call this function on an hourly basis */
    public function check_overdue_stuffs() {
        // do the checking here
    }

}

I have absolutely no idea on how to implement this. I have tried using sleep($seconds) but that was just a mess.

Any ideas for a perfectly awesome way to do this? A verbose example would be great. Thanks!

tereško
  • 58,060
  • 25
  • 98
  • 150
Kevin Lloyd Bernal
  • 355
  • 3
  • 8
  • 24

1 Answers1

3

This is pretty straight forward, but don't look to just keeping it in your PHP code. You need to use a cronjob script (linux) or windows equivalent (on the server where your software lives).

Here is a simple cron you would add to pull a specific cli method:

# hourly cron
0 * * * * php /www/ciWebsite/index.php [controller] [method] >/dev/null 2>&1

That way, it runs the controller + method you want and output is dumped into /dev/null

Try running /www/ciWebsite/index.php notifs check_overdue_stuff and see if it works (obviously update your path)

edit:

  • fixed an extra entry (I had cli = folder + controller + method)
Jakub
  • 20,418
  • 8
  • 65
  • 92
  • one extra thing to consider. depending on how your server is setup, if you execute the `php` command, CI may not recognize you are coming from the command line, due to its internal checks. on my server (godaddy) I execute my php cron jobs with `php-cli` to avoid these issues. – Andrew Brown Feb 24 '14 at 07:46