Laravel schedule fire only once on initialization - php

When i use php artisan schedule:run command the shuffleQuoteOfTheDay method is evaluated once and then it does not fire it every minute.
Kernel#schedule method
protected function schedule(Schedule $schedule)
{
$schedule->call('App\Services\MotivationalQuoteService#shuffleQuoteOfTheDay')->everyMinute();
}
MotivationalQuoteService#shuffleQuoteOfTheDay method
public function shuffleQuoteOfTheDay(){
$currentQuoteOfADay = $this->getQuoteOfTheDay();
$randomQuote = MotivationalQuote::where('quote_of_the_day',false)->inRandomOrder()->first();
$currentQuoteOfADay->update(['quote_of_the_day' => false]);
$randomQuote->update(['quote_of_the_day' => true]);
return $randomQuote;
}
It does work once but not every minute, i have created jobs table.Can someone help me with this?Thank you in advance.

See Task Scheduling:
Here is the only Cron entry you need to add to your server:
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
This Cron will call the Laravel command scheduler every minute. Then, Laravel evaluates your scheduled tasks and runs the tasks that are due.
Laravel's task scheduler does not stay in memory, it needs to be run every minute. It will then check which tasks need to be run in that minute and run them. When you run the task scheduler using PHP it just runs once, it needs cron to run it every minute.
If your dev machine is a linux machine/homestead then you could test this lcoally.
To add a cron job:
$ crontab -e
then add * * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1 to the crontab file and save it.
Do not forget to start/reload cron service.
On ubuntu:
$ sudo service cron reload

You need to setup cron so it could run the php artisan schedule:run command every minute.
From the docs:
When using the scheduler, you only need to add the following Cron entry to your server. If you do not know how to add Cron entries to your server, consider using a service such as Laravel Forge which can manage the Cron entries for you:
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
This Cron will call the Laravel command scheduler every minute. When the schedule:run command is executed, Laravel will evaluate your scheduled tasks and runs the tasks that are due.

Related

do i have to do something to have the schedule run automatically every minute?

I created this function but it only works once when I execute this command:
php artisan schedule: run
Should I have to do something to continue automatic? is it then triggered automatically when you enter the site only once and run it alone?
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$dias1 = \Carbon\Carbon::today()->subDays(5);
$dias2 = \Carbon\Carbon::today()->subDays(15);
$encomendasnaopagas = encomendas::where('estado', 1)->where('updated_at', '<', $dias1)
->join('distritos', 'encomendas.distrito', '=', 'distritos.id')
->update(['estado' => 5]);
$encomendasenviadas = encomendas::where('estado', 3)->where('updated_at', '<', $dias2)
->join('distritos', 'encomendas.distrito', '=', 'distritos.id')
->update(['estado' => 4]);
})->everyMinute();
}
This may be something you have missed in the documentation on keeping the scheduler running
https://laravel.com/docs/8.x/scheduling#defining-schedules
When using the scheduler, you only need to add the following Cron entry to your server. If you do not know how to add Cron entries to your server, consider using a service such as Laravel Forge which can manage the Cron entries for you:
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
This Cron will call the Laravel command scheduler every minute. When the schedule:run command is executed, Laravel will evaluate your scheduled tasks and runs the tasks that are due.
You will need to setup an entry in your crontab or similar cron management system.

Command schedule in Laravel

I'm trying to a execute scheduled command every five minutes in background. I use this code
protected function schedule(Schedule $schedule)
{
$schedule->command('read:mail')->cron('*/1 * * * * *')->sendOutputTo(storage_path().'/logs/output.txt')->withoutOverlapping();
}
I suppose that this code is fine, when i use php artisan scheduler:run command works, but doesn't work every five minutes in background. ¿Any idea?
If you want to use the scheduler you need to add a Cron entry in your server, this line will call the Laravel scheduler every minute and do the tasks.
There is the line you need to add to your Crontab:
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
In case you are using Ubuntu to edit your crontab you can run crontab -e and add the line on the bottom.
You can read more on the official docs about Scheduling.
If you are using Windows you can follow this Stackoverflow question to add a task to the Windows task scheduler.

Scheduling jobs in Laravel

I created a command in laravel "update:date" and "php artisan list" successfully lists it. I am able to execute it using "php artisan update:date"
Kernel.php:
protected $commands = [
'Snuba\Console\Commands\Inspire',
'Snuba\Console\Commands\UpdateDate'
];
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')
->hourly();
$schedule->command('update:date')
->everyMinute();
}
I configured it to run every minute as given above. Do I need to configure anything else ? I think laravel should automatically register it as cron task on ubuntu server.
I think laravel should automatically register it as cron task on
ubuntu server.
No, you have to start it like:
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
Starting The Scheduler
When using the scheduler, you only need to add the following Cron entry to your server. If you do not know how to add Cron entries to your server, consider using a service such as Laravel Forge which can manage the Cron entries for you
Reference
Another Answer
I just noticed:
Call to undefined method Illuminate\Console\Scheduling\Event::everyMinute()
I followed the documentation and thought this is a valid method call. I found that everyMinute() is no more valid in laravel 5+. So, I used cron expression instead.
Laravel's command scheduler allows you to fluently and expressively define your command schedule within Laravel itself. For more go through following link laravel task scheduling
If you have access to SSH
run command crontab -e
insert this line:
php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
which will run all your specified crons as there specified times.
If you have cpanel then under crontab add the above line on command
If ->everyMinute does not exist, please use ->cron('*/1 * * * * *') to set every minute. Or override class laravel.com/api/5.x/Illuminate/Console/Scheduling/Event.html
If cron job not working, run command crontab -e and add this line without single quote
'* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1'
After do this I think it works perfectly.

Task helpers not running Laravel, but cron is running every minute

I have a task I'm trying to run every day. In my Kernel.php I have the following command:
$schedule->command('emailProgram')->daily()->timezone('US/Central');
I'm my crontab I have:
* * * * * php /var/www/html/appname/artisan schedule:run >> /dev/null 2>&1
So, When I run php artisan schedule:run, or run it directly with php artisan emailProgram it runs as expected. But, its not running on its own using daily()/dailyAt() or otherwise. Lastly, if I remove daily() from the command in the kernel.php file:
$schedule->command('emailProgram')->timezone('US/Central'); it's running every minute, so its like there is some disconnect with the Laravel task helpers. This is my first time setting up Cron, and task management with Laravel so maybe I'm overlooking something simple. Any help would be really appreciated, thanks.

Laravel 5.3 Schedule Not working ( No scheduled commands are ready to run. )

The below is the schedule function
protected function schedule(Schedule $schedule)
{
$schedule->command('queue:work')
->everyMinute()
->withoutOverlapping();
}
Below is the cron for laravel
* * * * * /usr/local/bin/php /home/space/public_html/project/artisan schedule:run >> /home/space/public_html/project/public/op.txt 2>&1
But each time the cron outputs
No scheduled commands are ready to run.
queue:work is not getting executed, what am I doing wrong?
Deleting all the schedule files in storage/framework did this job.
The problem was the command was executed but was some error so never worked but the command was active preventing it to run again by the cron (because i used withoutOverlapping() ).

Categories