开发环境 Mac + homestead
过程:我在命令行下用 php artisan make:console创建了一个artisan command。代码如下
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代码如下
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 和”任务调度“关键字相关的帖子。但还是未找到解决办法。
请论坛有时间有能力的朋友告诉我一下原因,或者给我一个思路。???。