我对代码(命令行和php)的外观很感兴趣,其中每分钟或每小时都要盘点从mysql数据库中获得的商品销售数量,并在此基础上执行一些操作。我正在使用CodeIgniter for PHP,我将把我的控制器称为"cronControl"。
以下是到目前为止我对命令行部分(包括目录)的了解:
:htdocs TimPeterson$ * * * * * php index.php cronControl countSales
以下是cronControlphp部分:
class CronControl extends CI_Controller {
function countSales(){
$count=$this->db->query("SELECT stuff");
//do->stuff->based on $count;
$count="i counted 137 items";
file_put_contents("mylogfile.txt", $count);
}
}
当我在shell中键入以上命令时,我得到:
-bash: 404.php: command not found
看起来它正在评估我的根目录(我的404.php页面所在的目录)中的所有php脚本,而不仅仅是cronControl/countSales控制器。请注意,如果去掉5个星号,这个shell命令可以工作并将$count打印到mylogfile.txt中。
有什么想法吗?
问题解决了!!:关键是在crontab文件中键入命令以包括星号,但在shell中键入命令不包括星号
所以在crontab-e:中
* * * * * /usr/bin/php /applications/xampp/htdocs/index.php cronControl countSales
而在htdocs$中
/usr/bin/php /applications/xampp/htdocs/index.php cronControl countSales
一种快速写入文件的方法是使用file_put_contents,例如
file_put_contents("/tmp/mylogfile.txt","Counted $count'n", FILE_APPEND );
CodeIgniter有一个内置的日志记录机制用于此目的。
例如:
log_message('debug', 'Cron count is ' . $count);
请参阅http://codeigniter.com/user_guide/general/errors.html了解更多信息。
确保为dubugging启用了日志记录(在config.php中设置),并且可以将文件写入日志目录。
在Dan的大力协助下,这个问题现在得到了解决。键入命令时,关键是在crontab文件中包含星号,但在shell中不包含星号:
crontab -e: * * * * * /usr/bin/php /applications/xampp/htdocs/index.php cronControl countSales
而在htdocs 中
$ /usr/bin/php /applications/xampp/htdocs/index.php cronControl countSales