代码点火器 - 每小时方法调用



我正在为我的项目使用 CodeIgniter。我有这个障碍来自我..

我的控制器中有此方法,我想每小时调用一次。让我们将其作为:

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
    }
}

我完全不知道如何实现这一点。我试过使用sleep($seconds)但那只是一团糟。

有什么想法可以完美地做到这一点吗?一个冗长的例子会很棒。谢谢!

这很简单,但不要只考虑将其保留在您的 PHP 代码中。您需要使用 cronjob 脚本 (linux) 或 Windows 等效脚本(在您的软件所在的服务器上)。

这是一个简单的cron,你可以添加来拉取一个特定的cli方法:

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

这样,它运行所需的控制器 + 方法,输出将转储到/dev/null

尝试运行/www/ciWebsite/index.php notifs check_overdue_stuff,看看它是否有效(显然更新您的路径)

编辑

  • 修复了一个额外的条目(我有 cli = 文件夹 + 控制器 + 方法)

最新更新