简体   繁体   中英

Laravel Scheduling - Only everyMinute works

It seems that only everyMinute() and cron('* * * * *') are working for me. Any other methods like everyFiveMinutes, everyTenMinutes, daily, dailyAt etc, aren't working at all and always return "No scheduled commands are ready to run". My cron entry is always * * * * * so the other methods should work as well right? And yes; I've actually tried waiting for the other methods including daily, excluding yearly :P

Cron entry: * * * * * /opt/alt/php72/usr/bin/php /home/retracted/domains/retracted/artisan schedule:run >> /dev/null 2>&1

Schedule entry:

$schedule->call(function () {
    $stat = new Stat();
    $stat->users = User::count();
    $stat->reviews = Review::count();
    $stat->scholen = School::count();
    $stat->save();
})->daily();

So my questions: Why don't the other methods work? How do I make the other methods work, especially daily()?

So first you have to run your cronjob every minute that is correct. With that line you run your Laravel scheduler.

So i don't know the scheduler code but it's possible that the code runs only on that minute and not backwards.

So if you need a 5 minute cronjob you have to run your scheduler every minute and then define your duration in your scheduler task.

$schedule->call(function () {
    $stat = new Stat();
    $stat->users = User::count();
    $stat->reviews = Review::count();
    $stat->scholen = School::count();
    $stat->save();
})->everyFiveMinutes();

So with the function ->everyFiveMinutes(); you can run the scheduler every five minutes.

For laravel custom cron jobs to work you have to do the following:

  1. First setup an every minute cron by executing the command " crontab -e " and adding the following line * * * * * php /var/www/html/crontutorial/artisan schedule:run >> /dev/null 2>&1
  2. Configure the appropriate timezone on app/config.php eg 'timezone' => 'Europe/Berlin',
  3. Create a custom command that you want to execute at a specific time. If you don't know how to create custom commands please have a look at laravel cronjob scheduling tutorial

  4. Schedule custom crons in app/Console/Kernel.php by adding the following lines of code

     protected function schedule(Schedule $schedule) { $schedule->command('my:customcommand') ->cron('01 13 * * *'); } 

    The cron will run every day 1.13pm using the timezone configuration in app/config.php .

Did you try specifying with the timezone? Check the below working snippet from my project:

protected function schedule(Schedule $schedule)
    {
        try{

            $schedule->call(function (){
                (new MTSnapshot)->createSnapshot();
            })->timezone('Asia/Kolkata')->dailyAt('23:57');

            $schedule->call(function (){
                (new MTTransaction)->schedulerStatus();
            })->hourly();

            $schedule->call(function (){
                (new MTTransaction)->syncPendingTransactions();
                (new MTCommission)->processPendingCommissions();
            })->twiceDaily(1, 16); 

        } catch(\Throwable $t){
            Log::emergency($t);
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM