13

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
CJ Dennis
  • 4,226
  • 2
  • 40
  • 69
lighter
  • 2,808
  • 3
  • 40
  • 59

5 Answers5

34

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.

Markus Köhler
  • 764
  • 2
  • 8
  • 20
Grigoreas P.
  • 2,452
  • 25
  • 19
9

Laravel scheduler works with commands, not with controller methods:

  1. create command:
php artisan make:command PurchasePodcast
  1. edit command:
namespace App\Console\Commands;

use Illuminate\Console\Command;

class PurchasePodcast extends Command
{
    protected $name = 'purchase:podcast';

    public function fire()
    {
        // do stuff here
    }
}
  1. add command to Console\Kernel.php:
protected $commands = [
    'App\Console\Commands\PurchasePodcast',
];
  1. use command in scheduler:
$schedule->command('purchase:podcast')->hourly();
Limon Monte
  • 52,539
  • 45
  • 182
  • 213
  • I had add PATH on my cron, does not work? Because I use `$schedule->call(join('@', [ApiController::class, 'test_job']));`, and I use command `php artisan schedule:run`, it will run command. But I use original, it will tell me `No scheduled commands are ready to run.` – lighter Jun 05 '15 at 10:36
  • 2
    I modify my cron `* * * * * php /home/ubuntu/workspace/app/artisan schedule:run 1>> /dev/null 2>&1`, and my schedule is like yours `$schedule->command('purchase:podcast')->hourly();`, I try to run `php artisan schedule:run`, and I got this message `No scheduled commands are ready to run.`, this is why? If I modify my schedule to `$schedule->command('purchase:podcast')->cron('* */1 * * *');`, it will run. – lighter Jun 07 '15 at 03:27
  • @lighter interesting, can you please make another question? – Limon Monte Jun 07 '15 at 08:55
  • @limote I new a question, http://stackoverflow.com/questions/30691823/laravel-5-schedule-not-run-it – lighter Jun 07 '15 at 09:10
2

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
Irfandi D. Vendy
  • 894
  • 12
  • 20
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.

George Kastrinis
  • 4,924
  • 4
  • 29
  • 46
lydiacx
  • 11
  • 1
0

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