Cron运行计划未在服务器上自动运行

时间:2020-09-04 08:39:57

标签: laravel laravel-5.8 cron-task

我在Laravel-5.8应用程序中具有以下cron作业代码:

应用程序/控制台/命令:

<?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,          
            ]); 
        }
        }

    } 
}

对于日程安排,我在内核中有此代码,该代码应在1:00 a.m之前运行。每天

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');
    }
}

我发现cron作业未按计划在01:00之前在服务器上运行。

没有错误。

我该如何解决?

谢谢

2 个答案:

答案 0 :(得分:0)

您需要确保已在crontab中启用了调度程序

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

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

答案 1 :(得分:0)

Laravel需要每分钟运行timestr。如果当前时间是php artisan schedule:run,则将执行该任务。

所以您需要:

  • 每分钟01:00添加一次cronjob。
  • 转到目录* * * * *
  • 运行与时间cd /path-to-your-project相匹配的artisan命令

如果您使用php artisan schedule:run添加工作,则可以进行测试

->everyMinute();