Scheduling jobs in Laravel - php

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.

Related

Laravel schedule fire only once on initialization

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.

Laravel - Task Scheduling

I'm using Laravel 5.4 on a local WAMP Server. I wanted to perform a Task Scheduling but I'm not sure if I really understood how this works.
I created a command cronEmail and in the handle() function added code where I would get an Email.
In Kernel.php I added this:
protected $commands = [
'App\Console\Commands\cronEmail'
];
...
protected function schedule(Schedule $schedule)
{
$schedule->command('send:email')
->everyMinute();
}
So basically I want to get an email every minute. But how do I start this?
I tried entering:
php artisan schedule:run >> /dev/null 2>&1
or even
php C:\wamp64\www\seo-parser\artisan schedule:run >> /dev/null 2>&1
on my cmd but I always get:
The system cannot find the path specified.
If i enter php artisan schedule:run it will actually send an email but only once.
Did I understand the whole concept wrong? How do I do this properly?
Thank you,
Patrick
As stated in the official Laravel documentation you need to add the following line to your crontab.
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
You do this by typing "crontab -e" in the console. Paste the above line and replace the "/path-to-your-project" with the path to your project.
This results in a cronjob wich calls the "php artisan schedule:run" command every minute.
This requires you to run Linux though. If you need an alternative to crontab when running Windows you can start by reading here.

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.

Task schedular of laravel not working properly on server

I am able to execute individual artisan commands on server using putty cli. for eg.
php artisan inspire
and other custom commands related to database, they work fine while using putty. Also i put them in kernel.php 's schedule funtion like this
protected function schedule(Schedule $schedule){
$schedule->command('customcommand:execute')->everyMinute();
}
when i run this command using putty
php artisan schedule:run
it also works fine.
problem is that i am not able to execute schedule:run command via servers cron job.. command looks like this on server
php -d register_argc_argv=On /path/to/artisan schedule:run >> /dev/null 2>&1
funny thing is i am able to execute individual commands via servers cron job
i.e.
php -d register_argc_argv=On /path/to/artisan customcommand:execute >> /dev/null 2>&1
works as well...
only that schedule command is not working.
also it does not show any errors..
also if I dont add '-d register_argc_argv=On', i get exception 'ErrorException' with message 'Invalid argument supplied for foreach()
again here by server i mean cPanal, i have added this command under 'cron jobs'
The ini directive register_argc_argv was disabled on the php.ini and I needed to enable it explicitly on each call in order to accept argc and argv parameters. Luckily I easily solved this problem using exec instead of command
$schedule->exec('php -d register_argc_argv=On /path/to/artisan sms:bulk-send')
Use your command instead of sms:bulk-send Then it will be work.
* * * * * /usr/local/bin/ea-php71 /home/your_domain/artisan schedule:run >> /dev/null 2>&1
first ,
which php
to get which php you're using now
then change your cron job command like
* * * * * /path/to/your/php /path/to/your/project/artisan schedule:run
specify which php and project you are using
for example * * * * * /usr/local/bin/php /home/user/project/artisan schedule:run
it works for me

Custom Laravel Artisan command not available through cron job

I have custom Artisan commands that run locally as well as on my production server when I am SSH'd in, but are unavailable to any cron jobs. I've even tried running it as the user the cron job runs as and it works fine from my console.
When I run php artisan in the above settings, my custom commands are listed and available. However, they are not listed when I run php artisan as a cron job.
Furthermore, trying to run the custom command php artisan subjects:calculate as a cron job results in the following error:
[InvalidArgumentException]
There are no commands defined in the "subjects" namespace.
I was fighting with the same error and I found the solution.
First failed attempts
*/5 * * * * /usr/bin/php /home/mysite/public_html/artisan my:command
*/5 * * * * php /home/mysite/public_html/artisan my:command
Solution
*/5 * * * * /usr/local/bin/php /home/mysite/public_html/artisan my:command
Be sure to add the command to app/start/artisan.php file:
Artisan::add(new SubjectsCommand);
or if you are using the IOC container:
Artisan:resolve('SubjectsCommand');
Then run the CronJob from the folder of the app:
00 09-18 * * 1-5 php /path/to/yourapp/artisan subjects:calculate
or
00 09-18 * * 1-5 /usr/bin/php /path/to/yourapp/artisan subjects:calculate
At least for me that worked:
class Kernel extends ConsoleKernel
{
protected $commands = [
// Commands\YourCommand::class,
];
}
Just added my command to the list of command kernel.
You probable need to make sure artisan is running from the correct directory
cd /path/to/yourproject && php artisan subjects:calculate

Categories