Ubuntu16.04+crontab 定时任务不执?

crontab -l 显示任务为:

* * * * * php ~/Code/www/artisan schedule:run >> /dev/null 2>&1

Kernel代码部分为:

$schedule->command('check_article');

定时任务代码为:

$now = new Carbon('2017-06-08 00:00:00');
$day = $now->toDateString();
$tomorrow = new Carbon('2017-06-09');
$tomorrow = $tomorrow->toDateString();

$data =  Article::where('ptime', '>=',$day)
    ->where('ptime', '<', $tomorrow)
    ->where('is_check', '>', 0)
    ->get()->toArray();
if(count($data)<=0){
    $res = Article::where('ptime', '>=',$day)
        ->where('ptime', '<', $tomorrow)->count();
    $limit = $res*0.1 < 1 ? 1:  intval(round($res*0.1));
    $arr = Article::where('ptime', '>=',$day)
            ->where('ptime', '<', $tomorrow)
            ->orderBy(\DB::raw('RAND()'))
            ->limit($limit)
            ->get()->pluck('id')->toArray();
    Article::whereIn('id', $arr)->update(['is_check'=>1]);
}

我在命令控制台直接输入php ~/Code/www/artisan schedule:run 是可以执行的。但是crontab 的定时任务却不能执行。

关注微信公众号:后端时光
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
leo
最佳答案
  1. ~/Code/xxx 改为完整路径而不是 ~
  2. 确认php的路径在环境变量中,保险的做法是把php的路径也写全。

/usr/local/bin/php /home/vagrant/Code/xxx/artisan schedule:run >> /dev/null 2>&1

6年前 评论
讨论数量: 2
leo
  1. ~/Code/xxx 改为完整路径而不是 ~
  2. 确认php的路径在环境变量中,保险的做法是把php的路径也写全。

/usr/local/bin/php /home/vagrant/Code/xxx/artisan schedule:run >> /dev/null 2>&1

6年前 评论

@leo 谢谢大大。还真是路径问题

6年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!