Codeigniter cron 作业不工作
Codeigniter cron job not working
我有这样的 cron 作业:
/usr/bin/php /var/www/website/public_html/index.php className methodName
如果我 运行 它在终端它 运行s,但什么都不输出。如果我传递了错误的方法名称,它 运行 就会成功。如果我传递错误的 class 名称,它会输出网站 404 错误。
我还有一个路由将"en"添加到url,例如
http://www.website.com/en/home/index
这可能是问题所在吗?
我的 config.php 设置是:
$config['uri_protocol'] = 'AUTO';
$config['index_page'] = '';
通过 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) . '/';
3rd: 像这样执行你的 cron 作业:
/usr/bin/php /var/www/website/public_html/cli.php controller method
其中 /var/www/website/public_html/
是您服务器的主目录,您的 index.php
和 cli.php
.
的位置
备注:
for CI 3.0 你找到了必要的信息here
数据库: 您需要在控制器方法中提供数据库配置设置,因为 cron 作业只是执行控制器的方法。所以它对任何数据库设置一无所知!
$config['hostname'] = "localhost";
$config['username'] = "username_admin";
$config['password'] = "password";
//etc..
$this->db = $this->load->database($config, TRUE);
调试: 只需在您的 html 中添加一个 link 到 运行 您的控制器方法,如:index.php/controller/method
(删除一旦您的网站上线)
来源:非常helpful
您也可以 运行 没有 cli.php
的终端的 cron 文件。它不是强制性文件。
我是 运行nig cron 从我的服务器使用以下语法:
/usr/bin/php /var/www/project1/index.php controllerName methodName >> /var/log/project1/logFile.txt
我认为这是让 codeigniter 在 cron 作业下工作的最简单方法,
您可以在 cron 作业中使用 curl 来执行或 运行 在 cron 作业中使用 codeigniter
示例:
* * * * * /usr/bin/curl https://www.domain.com/controller/function
对我来说很好用....
-O /dev/null-q 命令行用于删除服务器中的日志文件...
wget -O /dev/null -q http://www.mani.com/poject-name/index.php/tools/sendPushNotification
我有这样的 cron 作业:
/usr/bin/php /var/www/website/public_html/index.php className methodName
如果我 运行 它在终端它 运行s,但什么都不输出。如果我传递了错误的方法名称,它 运行 就会成功。如果我传递错误的 class 名称,它会输出网站 404 错误。
我还有一个路由将"en"添加到url,例如
http://www.website.com/en/home/index
这可能是问题所在吗?
我的 config.php 设置是:
$config['uri_protocol'] = 'AUTO';
$config['index_page'] = '';
通过 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) . '/';
3rd: 像这样执行你的 cron 作业:
/usr/bin/php /var/www/website/public_html/cli.php controller method
其中 /var/www/website/public_html/
是您服务器的主目录,您的 index.php
和 cli.php
.
备注:
for CI 3.0 你找到了必要的信息here
数据库: 您需要在控制器方法中提供数据库配置设置,因为 cron 作业只是执行控制器的方法。所以它对任何数据库设置一无所知!
$config['hostname'] = "localhost";
$config['username'] = "username_admin";
$config['password'] = "password";
//etc..
$this->db = $this->load->database($config, TRUE);
调试: 只需在您的 html 中添加一个 link 到 运行 您的控制器方法,如:index.php/controller/method
(删除一旦您的网站上线)
来源:非常helpful
您也可以 运行 没有 cli.php
的终端的 cron 文件。它不是强制性文件。
我是 运行nig cron 从我的服务器使用以下语法:
/usr/bin/php /var/www/project1/index.php controllerName methodName >> /var/log/project1/logFile.txt
我认为这是让 codeigniter 在 cron 作业下工作的最简单方法,
您可以在 cron 作业中使用 curl 来执行或 运行 在 cron 作业中使用 codeigniter 示例:
* * * * * /usr/bin/curl https://www.domain.com/controller/function
对我来说很好用.... -O /dev/null-q 命令行用于删除服务器中的日志文件...
wget -O /dev/null -q http://www.mani.com/poject-name/index.php/tools/sendPushNotification