0

I've created a Task Scheduling from Laravel Documentation: https://laravel.com/docs/5.7/scheduling

I put it everyMinute() and it only execute once when I use that command and not executing every minute. I used cronhub.io to monitor it and the result was the one I execute the command.

enter image description here

This is the command I execute:

$ php artisan schedule:run
Running scheduled command: "E:\Laragon\bin\php\php-7.2.11-Win32-VC15-x64\php.exe" "artisan" update:weather > "NUL" 2>&1

App\Console\Kernel.php

<?php
...
class Kernel extends ConsoleKernel
{
...
protected function schedule(Schedule $schedule)
    {          $schedule->command('update:weather')->everyMinute()->thenPing("https://cronhub.io/ping/ce3d19a0-6c01-11e9-8ce3-9b563ae1be45");
    }

And on my App\Console\Commands\UpdateWeatherIcon.php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Enums\WeatherIcon;
use App\Weather;

class UpdateWeatherIcon extends Command
{
    protected $signature = 'update:weather';
    protected $description = 'Update Weather Icon every end of day';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $weather = new WeatherIcon();

        $current_weather = Weather::find(1);
        $current_weather->current_weather = $weather->getRandomValue();
        $current_weather->updated_at = \Carbon\Carbon::now();
        $current_weather->save();
    }
}

Does it only run once in windows 10? cause this is my first time using cron jobs.

Storm Spirit
  • 1,440
  • 4
  • 19
  • 42
  • If you run directly this command `php artisan schedule:run` in terminal. it always run only once bcoz its not scheduler... In windows you need to create batch file which run `php artisan schedule:run`. Open windows task scheduler, create new scheduler.. choose created batch file and run it every 1 minutes.. – ZeroOne May 01 '19 at 11:34
  • if I deploy that on server, and I think it will be on linux/etc, do I need to create a batch also? – Storm Spirit May 01 '19 at 11:36
  • in linux use crontab.. – ZeroOne May 01 '19 at 11:37
  • so those command in laravel are useless like `->everyMinute();`, `->hourly();` and etc. Because I still need set it on windows/linux side. I thought just run `php artisan schedule:run` will automatic create cronjobs. – Storm Spirit May 01 '19 at 11:39
  • not like that.. crontab will run every minutes and check your command in kernel whether to be execute or not by this `->everyMinute();, ->hourly();`.. maybe you can read about cronjob and https://laravel.com/docs/5.7/scheduling – ZeroOne May 01 '19 at 11:43
  • thanks a lot for this, so there is no wrong in my code right? only this I missed that I need to run `* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1` – Storm Spirit May 01 '19 at 11:46
  • https://stackoverflow.com/questions/30664310/laravel-5-schedule-not-working – Sergio Perez Dec 01 '21 at 23:03

1 Answers1

1

you most run this command

$ php artisan schedule:work

if you set cronjob on shared hosting you must add this command

cd /home/admin/domains/yourdomain.com/projectfolder && php artisan schedule:run >> /dev/null 2>&1 
helvete
  • 2,455
  • 13
  • 33
  • 37
  • Note: Be careful about jobs in queue that not processed(jobs table in database) because all of my notifications sent again to users after run command. – PouriaDiesel Mar 02 '23 at 20:24