Linux cron 定时任务用户不当导致的权限问题

由于项目是在服务器上开启cron定时任务后才出现的,猜测应该是cron的问题。查看nginx日志文件发现是laravel日志文件权限问题打不开。


  • 思考原因感觉不对啊,整个项目都用的www用户,phpnginx也是以www用户运行的。但是为什么日志文件的用户和用户组都是vmuer:vmuser(系统当前用户)
  • 查看后觉得肯定不是项目配置的问题,那么问题应该就是linux cron 的问题。
    输入 `crontab -l` 显示如下
    # minute (m), hour (h), day of month (dom), month (mon),
    # and day of week (dow) or use '*' in these fields (for 'any').# 
    # Notice that tasks will be started based on the cron's system
    # daemon's notion of time and timezones.
    # 
    # Output of the crontab jobs (including errors) is sent through
    # email to the user the crontab file belongs to (unless redirected).
    # 
    # For example, you can run a backup of all your user accounts
    # at 5 a.m every week with:
    # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
    # 
    # For more information see the manual pages of crontab(5) and cron(8)
    # 
    # m h  dom mon dow   command
    ` * * * * * /usr/bin/php7.1 /var/www/good/artisan schedule:run >> /dev/null 2>&1`
  • crontab 也是正常的好像没问题,但是突然想起cron添加计划任务的时候如果没有指定--user那么默认用户就是当前用户。所以也就解释为什么laravel的日志文件用户和用户组都变成当前用户了(计算任务里有写入日志,所以会产生日志文件)
    于是删除当前用户的计划任务,重新添加www用户的系统计划任务就行
    删除
    crontab -r
    新建www用户的计划任务
    crontab -u www -e

    OK!问题解决,还是自己不细心的问题!!!