PHP artisan + 命令能执行 PHP artisan schedule:run 却不能执行?

开发环境 Mac + homestead

过程:我在命令行下用 php artisan make:console创建了一个artisan command。代码如下

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Order;

class Automaric extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '尝试计划任务';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $orders = Order::where('id', '>', 6000)->get();
        foreach ($orders as $order) {
            $order->feedbackrecord = 5;
            $order->remark = 'test';
            $order->save();
        }
        $this->info('test');
        return true;
    }
}

然后我的kernel代码如下

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Commands\Inspire::class,
        Commands\Automaric::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('Automaric')->everyMinute();
    }
}

我在命令行下直接运行 php artisan test 得到了 Automaric类里handle()方法的预期结果。
我在 homestead 环境里编写了 crontab -e 加入了 * * * * * php /home/vagrant/Code/sample/artisan schedule:run >> /dev/null 2>&1 这行代码。得到 crontab: installing new crontab的结果。但是却没有得到 php artisan test的结果。我接着在命令行下运行 php artisan schedule:run 得到 Running scheduled command: '/usr/bin/php7.0' 'artisan' Automaric > '/dev/null' 2>&1 & 但是数据库还是没有改变。
我尝试把 crontab -e 的 php 改成绝对路径 /usr/bin/php 但结果也不行。我搜索了谷歌与百度。论坛也搜索了 cron 和”任务调度“关键字相关的帖子。但还是未找到解决办法。
请论坛有时间有能力的朋友告诉我一下原因,或者给我一个思路。???。

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 4
leo

schedule的command方法的参数应该是test

6年前 评论

@leo 谢谢你,昨晚卡到一点多,关了电脑还想了好久,今天回到家试了一下一下成功了。真的谢谢你。

6年前 评论

php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
我是手动添加到 cron 的文件里的,我是阿里的 centos7,按道理来说这个不是可以自动添加到 cron 里面吗?
@leo

4年前 评论
Epona

@fansj 手动添加的

4年前 评论

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