Laravel 5 schedule not working - php

My laravel version is 5.0.28, I build on cloud9, and I added this command to my cron:
#!/bin/bash
PATH=/usr/bin
* * * * * php /home/ubuntu/workspace/app/artisan scheduled:run 1>> /dev/null 2>&1
I added this code on my Kernel.php. I referenced this site: https://laravel-news.com/2014/11/laravel-5-scheduler/
<?php namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Http\Controllers\ApiController;
class Kernel extends ConsoleKernel {
protected $commands = [
'App\Console\Commands\Inspire',
];
protected function schedule(Schedule $schedule)
{
$schedule->call('ApiController#test_job')->hourly();
}
}
I waited and it still didn't work, so I tried to use the command php artisan schedule:run, and I got: No scheduled commands are ready to run.
I searched and found this answer: Laravel 5 "Class does not exist" when using the scheduler
So I modified my code. Also, this code had no specified time, so I modified my cron to specify a time, but it still doesn't work. I have no more ideas. please help. Thanks.
code
$schedule->call(join('#', [ApiController::class, 'test_job']));
cron
0 0,3,6,9,12,15,18,21 * * * php /home/ubuntu/workspace/app/artisan schedule:run 1>> /dev/null 2>&1
30 1,4,7,10,13,16,19,22 * * * php /home/ubuntu/workspace/app/artisan schedule:run 1>> /dev/null 2>&1

first to Test if cron is running in your server or localhost type:
> sudo service cron status
if not installed:
> sudo apt-get install cron
to enable laravel's scheduler:
> crontab -e
and you can select an editor if not vim opens directly. Be sure to enter there this line at the bottom:
* * * * * php /path_from_root_to_laravel_proj_folder/artisan schedule:run 1>> /dev/null 2>&1
to Test if you have setup inside laravel the scheduler right, run this from your projects folder:
>php artisan schedule:run
this should execute the tasks and tell you what is doing.

Laravel scheduler works with commands, not with controller methods:
create command:
php artisan make:command PurchasePodcast
edit command:
namespace App\Console\Commands;
use Illuminate\Console\Command;
class PurchasePodcast extends Command
{
protected $name = 'purchase:podcast';
public function fire()
{
// do stuff here
}
}
add command to Console\Kernel.php:
protected $commands = [
'App\Console\Commands\PurchasePodcast',
];
use command in scheduler:
$schedule->command('purchase:podcast')->hourly();

In my case, the scheduler actually run but encountered an error because of lower php version, beside artisan path (which is your project folder), I had to set the php path as below:
* * * * * /path_to_php_folder/bin/php /path_from_root_to_laravel_proj_folder/artisan schedule:run 1>> /dev/null 2>&1

There are several aspects to find the cause:
First, check whether the 'timezone' in config/app.php is set properly. Laravel will reset the timezone even though you already configured it in php.ini.
Secondly, check that crontab is working as expected. When you get the message "No schedule to be ready", it means your crontab is running and can detect the php and artisan command.

In order to complete #limonte's answer the create Console Command is the following:
php artisan make:console CampaignsCollect --command=campaigns:collect
Reference here: link

Related

Laravel 5 schedule issues - php artisan schedule:run works but actual schedule does not

Laravel version 5.7, PHP version 7.2 running on Apache2
The command in my crontab is as follows:
* * * * * php /var/www/dev-site/artisan schedule:run >> /dev/null 2>&1
My kernel is as follows:
<?php
namespace App\Console;
use App\Console\Commands\ImportInvoiceBatch;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
ImportInvoiceBatch::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('import:invoice-batch')->everyMinute()->withoutOverlapping();
}
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
Running my defined command as "php artisan import:invoice-batch" works correctly, and even running "php artisan schedule:run" recognizes everything and runs the task as expected, but the scheduler is not being executed ever minute as should be happening. Is there anything I am missing? Thanks in advance.
Try to change working directory to app root before running artisan:
* * * * * cd /var/www/dev-site/artisan && php artisan schedule:run >> /dev/null 2>&1
Also you could try to add output path for debuging:
* * * * * cd /var/www/dev-site/artisan && php artisan schedule:run >> /var/log/crontab/artisan.log 2>&1

My laravel's task scheduling not working properly

Trying to do task scheduling but not working properly. I set the time and wrote the commands but not running.
Here is my commands;
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\allestates;
protected $signature = 'bot:mitsui';
Also my Kernel is here:
protected $commands = [
//
Commands\mitsui::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('bot:mitsui')->everyFiveMinutes();
}
What am I missing here? Thanks for helping me out!
Are you running the schedule cron.? You need to add this into your cpanel crons or cronjob.
php {project_path}/artisan schedule:run > /dev/null 2>&1
If not add the above command to your cron jobs and run it everyminute.
Here the steps if you have root access,
crontab -e
//Add follow line to crontab
* * * * * php {project_path}/artisan schedule:run >> /dev/null 2>&1

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.

Starting the Laravel cron job on a Mac

I have a command scheduled in the Laravel 5.4 scheduler and would like to start the Laravel cron on Mac OS X El Capitan.
app/Console/Kernel.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
'App\Console\Commands\GetToken'
];
protected function schedule(Schedule $schedule) {
$schedule->command('gettoken')->everyMinute();
}
protected function commands() {
require base_path('routes/console.php');
}
}
My GetToken.php makes an API call and then a DB change. I believe that this is working properly, as I can run the task directly from the cli using:
php /path/to/project/artisan schedule:run 1>> /dev/null 2>&1
To edit my cron file I use:
env EDITOR=nano crontab -e
I then add:
* * * * * php /path/to/project/artisan schedule:run >> /dev/null 2>&1
I save with ctrl+o and exit with ctrl+x.
Re-editing the file shows that the changes have saved.
Running crontab -l shows the text that I entered into the crontab file.
My cron never runs. I can only get it to run once by running manually using the command I mentioned above.
Not directly answering your question, but proposing another solution:
If you want to set up cron jobs for your development environment, it's best to use Homestead, for its Linux standards compliance.
For small projects that i develop directly inside macOS, i run the following command inside the project root (in a separate terminal tab) to have my jobs run every minute:
while true; do php artisan schedule:run; sleep 60; done
This helps to make sure, the cron jobs are only run while i'm developing. When i'm done, i Ctrl+C that command and can be sure nothing unexpected happens while i'm not watching.
Plus it gives me the freedom to adjust the interval, by simple choosing another number of seconds for the sleep command. This can save time when developing.
Update Laravel 8.x
Laravel now offers the above as a single artisan command:
php artisan schedule:work

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