Search code examples
phplaravellaravel-5scheduled-taskscron-task

Laravel schedular works manually but not automatically?


I have this in my Kernal.php:

$schedule->call('removeTemporaryFiles')->everyMinute();

When I hit php artisan schedule:run it works like charm. But I also ran:

 * * * * * php /var/www/html/archive/artisan schedule:run >> /dev/null 2>&1

But it is not running automatically. I have waited more than a minute but it is still not running. What am I doing wrong?

And where is the main machine cron saved? The one that runs every minute and calls artisan schedule:run?


Solution

  • In order for Schedules to run, you need first to add the cron job to your cron table. Run this command

    sudo crontab -e
    

    Then choose your preferred editor.

    Then add the below line:

     * * * * * php /var/www/html/archive/artisan schedule:run >> /dev/null 2>&1
    

    Finally in your Kernel.php you add the schedule:

    $schedule->command(<artisan command>)->everyMinute();
    

    The documentation is perfectly detailing it.