一,
php artisan make:command Apple
二,在App\Console\Commands下,修改Apple.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class Apple extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:apple';
/**
* The console command description.
*
* @var string
*/
protected $description = 'test command use apple';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
Log::info("command test topaz haha");
}
}
三,
修改App\Console\Kernel.php
<?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\Apple::class,//添加此处
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('command:apple')
->everyMinute();//修改此处
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
四.
执行 crontab -e
添加定时任务如下
*/1 * * * * /usr/bin/php7.2 /apple/laravel/artisan schedule:run >>/dev/null 2>&1
意思是每一分钟,执行一次。
查看日志表如下:
来源:CSDN
作者:php_M
链接:https://blog.csdn.net/qq_26582901/article/details/104041007