参考:https://learnku.com/docs/laravel/5.8/scheduling/3924#scheduling-artisan-commands (文档)
https://www.jianshu.com/p/99baaffcec18

原理:就是crontab增加个以每分钟或者每隔多长时间触发执行laravel项目设置定时任务

设置crontab每分钟触发

* * * * * cd /www/test && php artisan schedule:run >> /dev/null 2>&1

然后在\App\Console\Teacher.php

<?php

namespace App\Console\Operate;

use App\Common\Base\BaseCommand; use App\Modules\Report\DTO\ReportDateDTO; use App\Modules\Report\Services\ReportMainService;

class Teacher extends BaseCommand {

protected $signature = 'reportMain:generate {module-code-time} {--dto=}';

protected $description = 'reportMain generate
                            module-code-time 选择 day或week或month
                            --dto 可传可不传,例:--dto=20160904,20200415,20200621 (dataId,startAt,endAt)
';

public $reportMainService;

public function __construct(ReportMainService $reportMainService)
{
    parent::__construct();
    $this-&gt;reportMainService = $reportMainService;
}

public function handle()
{
    $moduleCodeTime = $this-&gt;argument('module-code-time');
    if(!in_array($moduleCodeTime,['day','week','month'])){
        $this-&gt;error('失败:module-code-time参数错误');
        return;
    }

    $moduleCode = 'operate_teacher_' . $moduleCodeTime;

    $ops = $this-&gt;options();
    $dtoString = $ops['dto']??null;

    $dto = null;
    if(!is_null($dtoString)){
        $dto = new ReportDateDTO();
        $dtoArr = explode(',',$dtoString);

        $dto-&gt;dateId = $dtoArr[0];
        $dto-&gt;startAt = $dtoArr[1];
        $dto-&gt;endAt = $dtoArr[2];
    }

    $this-&gt;reportMainService-&gt;generate($moduleCode, $dto);
    $this-&gt;info('生成结束');
    return;
}

}

上面的是command代码,触发方式:php artisan reportMain:generate week --dto=20160904,20200415,20200621
这边-dto格式要注意
然后就是正题每隔一分钟触发我好几个command命令了,在\App\Console\Kernel修修改改

<?php

namespace App\Console;

use App\Console\Operate\Teacher; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel { /** * The Artisan commands provided by your application. * * @var array */ protected $commands = [ Teacher::class,// 这边加下 ];

/**
 * Define the application's command schedule.
 * 下面就是调用的command的了
 * @param  Schedule  $schedule
 * @return void
 */
protected function schedule(Schedule $schedule)
{
    $schedule-&gt;command('reportMain:generate day')
        -&gt;description('每日凌晨1点执行')
        -&gt;dailyAt('01:00')
        -&gt;withoutOverlapping()
        -&gt;onOneServer()
        -&gt;runInBackground();

    $schedule-&gt;command('reportMain:generate week')
        -&gt;description('每周一凌晨1点执行')
        -&gt;weeklyOn(1, '01:00')
        -&gt;withoutOverlapping()
        -&gt;onOneServer()
        -&gt;runInBackground();

    $schedule-&gt;command('reportMain:generate month')
        -&gt;description('每月一号凌晨1点执行')
        -&gt;monthlyOn(1, '01:00')
        -&gt;withoutOverlapping()
        -&gt;onOneServer()
        -&gt;runInBackground();

}

/**
 * Register the commands for the application.
 *
 * @return void
 */
protected function commands()
{
    $this-&gt;load(__DIR__.'/Commands');


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

}

可测试