4

I have a Laravel 12.39 project (on PHP 8.3) on a shared hosting with cPanel access, and in the cron section, I have the following setting to run artisan schedule:run, but it isn't working. This was an upgrade from the Laravel 10 project, where the server crons were the same and worked.

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

In my app/Console/Kernel.php class, I have the following method and command:

<?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');
    }
}

Is the above cron set correctly at the cPanel, or is it an issue in my Laravel 12 code that I must be calling? As mentioned above, I´ve upgraded from Laravel 10 (where it was running) to Laravel 12.39, but it isn´t running.

Thanks in advance for any help on this issue.

2
  • Thanks for the contribution. Unfortunately, it is not working either. Commented Nov 22 at 16:17
  • Why not try it out and see what happens? You can also run that command manually on the shell to check this. Not piping the cron output into /dev/null might also yield some errors. Commented 2 days ago

1 Answer 1

5

In laravel 12, Kernel.php is not used anymore
You need to move your code to

routes/console.php

and the code will look like this in the 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();
}

To check your schedule list, run this command

php artisan schedule:list 

You're supposed to see your cron job and when it will be run next time

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, and I didn´t realize that. Nevertheless, I moved the commands after your code, and I get the error message ERROR: Non-static method Illuminate\Console\Scheduling\Schedule::command() cannot be called statically. Have to dig more into the new way Laravel 12 runs the commands.
After changing the Schedule call to ´´use Illuminate\Support\Facades\Schedule;``it is now working 😀🙏🙏 Thanks!
Sorry, forgot to add this line Thanks for your edit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.