Laravel 定时任务

创建定时任务

test任务名

php artisan make:command Cron/Test

Test.php文件里写逻辑

<?php

namespace App\Console\Commands\Cron;

use Illuminate\Console\Command;

class Test extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cron:test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '测试';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //需要执行的逻辑
    }
}

App\Console\Kernel设置执行时间

<?php

namespace App\Console;

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 = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        //此任务每分钟执行一次
        $schedule->command('cron:test')->everyMinute();
        //在自定义Cron调度上运行任务
        //$schedule->command('cron:test')->cron();
        //每五分钟运行一次任务
        //$schedule->command('cron:test')->everyFiveMinutes();
        //每十分钟运行一次任务
        //$schedule->command('cron:test')->everyTenMinutes();
        //每三十分钟运行一次任务
        //$schedule->command('cron:test')->everyThirtyMinutes();
        //每小时运行一次任务
        //$schedule->command('cron:test')->hourly();
        //每天凌晨零点运行任务
        //$schedule->command('cron:test')->daily();
        // 每天12:00运行任务
        //$schedule->command('cron:test')->dailyAt('12:00');
        // 每天1:00 & 13:00运行任务
        //$schedule->command('cron:test')->twiceDaily(1, 13);
        // 每周运行一次任务
        //$schedule->command('cron:test')->weekly();
        // 每月运行一次任务
        //$schedule->command('cron:test')->monthly();
        // 每月1号12:00运行一次任务
        //$schedule->command('cron:test')->monthlyOn(1, '12:00'); 
        // 每个季度运行一次
        //$schedule->command('cron:test')->quarterly(); 
        // 每年运行一次
        //$schedule->command('cron:test')->yearly(); 
        // 设置时区
        //$schedule->command('cron:test')->timezone('America/New_York');

        // 每周星期一
        //$schedule->command('cron:test')->mondays();
        // 每周星期二
        //$schedule->command('cron:test')->tuesdays();
        // 每周星期三
        //$schedule->command('cron:test')->wednesdays();
        // 每周星期四
        //$schedule->command('cron:test')->thursdays();
        // 每周星期五
        //$schedule->command('cron:test')->fridays();
        // 每周星期六
        //$schedule->command('cron:test')->saturdays();
        // 每周星期天
        //$schedule->command('cron:test')->sundays();

    }

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

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

执行定时任务命令

//单条任务执行命令
php artisan cron:test
//执行多个任务命令
php artisan schedule:run

 1,320 total views,  4 views today

Revisions

Comments are closed.