【发布时间】:2012-03-21 17:50:11
【问题描述】:
我对代码(命令行和 php)的样子很感兴趣,您每分钟或每小时都会清点从 mysql 数据库获得的商品销售数量,并据此执行一些操作。我正在为 PHP 使用 CodeIgniter,我将我的控制器称为“cronControl”。
这是我目前所拥有的命令行部分(包括目录):
:htdocs TimPeterson$ * * * * * php index.php cronControl countSales
这是 cronControl php 部分:
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
【问题讨论】:
-
好的,在 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 感谢大家的帮助!蒂姆
标签: php mysql codeigniter command-line cron