Kernel.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\Console;
  3. use Illuminate\Console\Scheduling\Schedule;
  4. use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
  5. class Kernel extends ConsoleKernel
  6. {
  7. /**
  8. * 定义应用的自定义 Artisan 命令
  9. *
  10. * @var array
  11. */
  12. protected $commands = [
  13. Commands\DailyTask::class,
  14. ];
  15. /**
  16. * Define the application's command schedule.
  17. *
  18. * @param \Illuminate\Console\Scheduling\Schedule $schedule
  19. * @return void
  20. */
  21. protected function schedule(Schedule $schedule)
  22. {
  23. // $schedule->command('inspire')->hourly();
  24. // 每天12:00执行
  25. $schedule->command('task:daily')->dailyAt('12:00')
  26. ->timezone('Asia/Shanghai') // 设置时区,可选
  27. ->runInBackground() // 在后台运行,可选
  28. ->withoutOverlapping() // 防止任务重叠执行
  29. ->appendOutputTo(storage_path('logs/daily-task.log')) // 输出到日志文件
  30. ->onOneServer(); // 如果使用多服务器,确保只在一台服务器上运行
  31. }
  32. /**
  33. * Register the commands for the application.
  34. *
  35. * @return void
  36. */
  37. protected function commands()
  38. {
  39. $this->load(__DIR__ . '/Commands');
  40. require base_path('routes/console.php');
  41. }
  42. }