-2

I have this cron job code in Laravel-5.8 application:

App/Console/Commands:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Hr\HrDesignation;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;
use Exception;
use Notification;
use GuzzleHttp\Client; 
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;


class UpdateCreateDesignation extends Command
{
    protected $signature = 'updatecreatedesignations';

    protected $description = 'Update or Create Designation';

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

    public function handle()
    {
        $client = new Client();
        $res = $client->request('GET','https://api.cloudjkk.net/jkkapp/profile/all-designations', [
        'query' => ['key' => 'dddddd']
    ])->getBody();

        $clientdatas = json_decode($res->getContents(), true);    
        
        foreach($clientdatas as $clientdata)
        {        
              if ($clientdata['job_title']) {
            $designation = HrDesignation::updateOrCreate([
                'designation_name' => $clientdata['job_title']
            ],
            [
                'description'       => $clientdata['job_description'],
                'company_id'        => 1,          
            ]); 
        }
        }

    } 
}

For the schedule, I have this code in the kernel which is supposed to run by 1:00a.m. every day

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\UpdateCreateDesignation',
    ];

    protected function schedule(Schedule $schedule)
    {       
        $schedule->command('updatecreatedesignations')
                ->dailyAt('01:00'); 
                                                                                                     
    }

    protected function commands()
    {

        require base_path('routes/console.php');
    }
}

I observe that the cron job is not running on the server as scheduled by 01:00.

There is no error.

How do I resolve this?

Thanks

lagbox
  • 48,571
  • 8
  • 72
  • 83
mikefolu
  • 1,203
  • 6
  • 24
  • 57
  • 1
    Do you have a cronjob scheduled? – Ron van der Heijden Sep 04 '20 at 08:40
  • @RonvarderHeijden - Please how do you mean. That's that I have. From my code, see the schedule in kernel.php $schedule->command('updatecreatedesignations') ->dailyAt('01:00'); – mikefolu Sep 04 '20 at 08:46
  • "When using the scheduler, you only need to add the following Cron entry to your server ... " - [Laravel 5.8 - Task Scheduler - Introduction - Starting the Scheduler](https://laravel.com/docs/5.8/scheduling#introduction) ... the docs are trying to tell you what you need to do – lagbox Sep 04 '20 at 08:49

2 Answers2

0

You will need to make sure that you have enabled the scheduler in crontab

https://laravel.com/docs/7.x/scheduling#introduction

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Spholt
  • 3,724
  • 1
  • 18
  • 29
  • When I tried this: > root@MyServer:/var/www/html/jkkappt# php artisan schedule:run I got this: > No scheduled commands are ready to run. – mikefolu Sep 04 '20 at 08:52
  • That's to be expected. it isn't 01:00 in morning is it lol. If you are hosting on a linux server, have you checked that you have that entry with your crontab? You can edit this with the `crontab -e` command line command – Spholt Sep 04 '20 at 08:56
  • Please how do I do that – mikefolu Sep 04 '20 at 09:01
  • It's in my comment, `crontab -e` – Spholt Sep 04 '20 at 09:03
0

Laravel needs to run the php artisan schedule:run every minute. If the current time is 01:00, that task is executed.

So you need to:

  • Add a cronjob every minute * * * * *.
  • Go to the directory cd /path-to-your-project
  • Run the artisan command that will match the time php artisan schedule:run

You can test this if you add a job with ->everyMinute();

$schedule->command('increment-db-int')
    ->everyMinute(); 
Ron van der Heijden
  • 14,803
  • 7
  • 58
  • 82