1、创建command文件
php artisan make:command 生成的文件名
2、进入生成的文件编写定时任务要执行的操作(这里以记录日志展示)
<?php
namespace App\Console\Commands\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class HomeCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'four'; //定义名字
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
Log::info('当前时间:'.date('Y-m-d H:i:s'));
}
}
3、进入Kernel.php文件调用生成的文件
<?php
namespace App\Console;
use App\Console\Commands\Commands\HomeCommand;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands=[
HomeCommand::class //后面自己加入,根据你定义的文件
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
$schedule->command('four')->everyMinute(); //定时
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
4、
通过
php artisan schedule:work
命令执行(一直执行,手动停止)
php artisan schedule:run
两个命令不同(执行一次)
更多时间配置可以去官方手册看
版权声明:本文为dacong_ya原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。