通过CLI(命令行界面)为 cron-jobs 准备 CodeIgniter 2.x 的步骤:
1st:创建您的根index.php 文件的副本并将其保存在您的根中为cli.php
2nd: 在您的 cli.php 中将 <?php 替换为以下代码:
#!/usr/local/bin/php
<?php
/* override normal limitations */
set_time_limit(0);
ini_set('memory_limit', '256M');
/* deny direct call from web browser */
if (isset($_SERVER['REMOTE_ADDR'])) die('Permission denied.');
/* constants */
define('CMD', 1);
/* manually set the URI path based on command line arguments... */
unset($argv[0]); /* except the first one */
$_SERVER['QUERY_STRING'] = $_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'] = '/' . implode('/', $argv) . '/';
第 3 次: 像这样执行你的 cron 作业:
/usr/bin/php /var/www/website/public_html/cli.php controller method
其中/var/www/website/public_html/ 是您服务器的主目录,即您的index.php 和cli.php 的位置。
注意事项:
对于 CI 3.0,您可以找到必要的信息 here
数据库:您需要在控制器方法中提供数据库配置设置,因为 cron 作业只是执行控制器的方法。所以它对任何数据库设置一无所知!
$config['hostname'] = "localhost";
$config['username'] = "username_admin";
$config['password'] = "password";
//etc..
$this->db = $this->load->database($config, TRUE);
调试:只需在您的 html 中添加一个链接即可运行控制器的方法,例如:index.php/controller/method(网站上线后删除)
来源:非常helpful