代码之家  ›  专栏  ›  技术社区  ›  McRui

Laravel 12中的cron设置出现问题,无法正常工作

  •  3
  • McRui  · 技术社区  · 6 月前

    我有一个Laravel 12.39项目(在PHP 8.3上)在一个具有cPanel访问权限的共享主机上,在cron部分,我有以下设置来运行artist schedule:run,但它不起作用。这是Laravel 10项目的升级,其中服务器cron是相同的并且可以工作。

    *   *  *  *  * /usr/local/bin/php /home/my-project/public_html/artisan schedule:run > /dev/null 2>&1
    

    在我的 app/Console/Kernel.php 类,我有以下方法和命令:

    <?php
    
    namespace App\Console;
    
    use Illuminate\Console\Scheduling\Schedule;
    use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
    
    class Kernel extends ConsoleKernel
    {
        /**
         * Define the application's command schedule.
         */
        protected function schedule(Schedule $schedule): void
        {
            // Continuous
            // Queue
            $schedule->command('queue:work --tries=3')->runInBackground()->withoutOverlapping()->evenInMaintenanceMode()->everyMinute();
        
            if(app()->isProduction()) {
               // Every five minutes
               // Clear views
               $schedule->command('view:clear', ['--quiet'])->withoutOverlapping()->everyFiveMinutes();
            }
        }
    
        /**
         * Register the commands for the application.
         */
        protected function commands(): void
        {
            $this->load(__DIR__ . '/Commands');
        
            require base_path('routes/console.php');
        }
    }
    

    提前感谢您在这个问题上提供的任何帮助。

    1 回复  |  直到 6 月前
        1
  •  1
  •   McRui    6 月前

    在laravel 12中,不再使用Kernel.php
    您需要将代码移动到

    routes/console.php
    

    在console.php中,代码将如下所示

    use Illuminate\Support\Facades\Schedule;
    
        Schedule::command('queue:work --tries=3')
        ->name('queue:work')
        ->withoutOverlapping()
        ->evenInMaintenanceMode()
        ->runInBackground()
        ->everyMinute();
    
    if (app()->isProduction()) {
        Schedule::command('view:clear', ['--quiet'])
            ->name('view:clear')  
            ->withoutOverlapping()
            ->everyFiveMinutes();
    }
    

    要检查您的日程安排列表,请运行以下命令

    php artisan schedule:list