Question

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!

Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top