php - 关于crontab执行yii框架控制台应用的问题?

 mobiledu2502880047 发布于 2022-11-17 00:18

我就直接上代码了:
【crontab】星/1 星 星 星 星 php /www/myweb/protected/yiic.php Test index >> /root/test.log

【yiic.php】
$yiic = dirname(__FILE__) . '/../framework/yiic.php';
$config = dirname(__FILE__) . '/config/console.php';
@putenv('YII_CONSOLE_COMMANDS = ' . dirname(__FILE__) . '/commands');
require_once($yiic);

【TestCommand.php】
class TestCommand extends CConsoleCommand{

public $defaultAction = 'index';
public function actionIndex(){
    $tm  = '[' . date('Y-m-d H:i:s', time()) . ']';
    $msg = 'This is a Test Command!';
    $res = $tm . "\t" . $msg . "\n";
    echo $res;
}// END actionIndex

}// END CLASS

【问题?】每分钟执行这个定时任务的时候,不能写入文件!

【ps:】为了测试,给足权限:-rwxrwxrwx 1 root root 4 Dec 3 14:09 test.log

手动执行:php /www/myweb/protected/yiic.php Test index >> /root/test.log
可以正常写入文件到 test.log 中。
[root@xxx ~]# tail -f test.log
[2016-12-03 15:01:58] This is a Test Command!
[2016-12-03 15:02:16] This is a Test Command!

crontab 查看日志一切正常,并每分钟都在执行中。。。

但,
星/1 星 星 星 星 php /www/myweb/protected/yiic.php Test index >> /root/test.log 就是写不进去东西。。。。????

(星/1 星 星 星 星 ,因为我一写星号,就被这个编辑器过滤了,就用这种方式代替!)
求助高人!!!

1 个回答
  • 这个是linux权限问题,首先这个完全没有必要给那么高的权限。linux执行文件中会根据文件的权限去执行,比如:当前是root权限,但是一个人文件没有执行权限,那么root也一样无法执行这个文件。
    你吧日志放到PHP文件当前目录或者项目的日志目录里,让这个PHP文件去写这个日志文件,就可以操作了。
    你原来的PHP文件没有root目录的权限,所以无法写,当然你也不要给他那么高的权限,后果你应该知道。

    2022-11-17 03:23 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有