laravel8 定时任务并写入日志
1、创建command文件
php artisan make:command 生成的文件名
2、进入生成的文件编写定时任务要执行的操作(这里以记录日志展示)
<?php namespace AppConsoleCommandsCommands; use IlluminateConsoleCommand; use IlluminateSupportFacadesLog; 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 AppConsole; use AppConsoleCommandsCommandsHomeCommand; use IlluminateConsoleSchedulingSchedule; use IlluminateFoundationConsoleKernel as ConsoleKernel; class Kernel extends ConsoleKernel { protected $commands=[ HomeCommand::class //后面自己加入,根据你定义的文件 ]; /** * Define the applications command schedule. * * @param IlluminateConsoleSchedulingSchedule $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
两个命令不同(执行一次)
更多时间配置可以去官方手册看
下一篇:
Linux 安装Mysql 详细教程