美文网首页
laravel 设置定时任务(任务调度)

laravel 设置定时任务(任务调度)

作者: 刘禹锡_c886 | 来源:发表于2020-11-21 00:52 被阅读0次

1)创建数据表

CREATE TABLE `test_task` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `number` int(11) DEFAULT NULL,
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=27 DEFAULT CHARSET=utf8;

2)定义调度:在App\Console\Commands下创建Test.php

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;

class Test extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'lesson:testTask';     // 任务名,testTask是自定义

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

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

    /**
     * Execute the console command.
     *
     * @param Request $request
     * @return mixed
     */
    public function handle(Request $request)
    {
        //TODO::业务逻辑
        DB::table('test_task')->insert(
            [
                'number' => rand(100000000,999999999),
            ]
        );
    }
}

3)编辑 app/Console/Kernel.php 文件,将新生成的类进行注册:
$schedule->command('lesson:testTask')->everyMinute(); //每分钟执行testTask

<?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 = [
        // 加载注册
        \App\Console\Commands\Test::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('lesson:testTask')->everyMinute();
        // $schedule->command('inspire')
        //          ->hourly();
    }

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

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

4)查看定时任务
crontab -l
创建定时任务
crontab -e

* * * * * /www/server/php/73/bin/php /www/wwwroot/project/artisan schedule:run >> /dev/null 2>&1
#/www/server/php/73/bin/php 是php的安装绝对路径
#/www/wwwroot/project/artisan 是laravel安装根目录下的artisan文件

常用调度频率设置

->cron('* * * * *');            自定义 Cron 时间表执行任务
->everyMinute();                每分钟执行一次任务
->everyFiveMinutes();           每五分钟执行一次任务
->everyTenMinutes();            每十分钟执行一次任务
->everyFifteenMinutes();        每十五分钟执行一次任务
->everyThirtyMinutes();         每三十分钟执行一次任务
->hourly();                     每小时执行一次任务
->hourlyAt(17);                 每小时第 17 分钟执行一次任务
->daily();                      每天午夜执行一次任务
->dailyAt('13:00');             每天 13 点执行一次任务
->twiceDaily(1, 13);            每天 1 点 和 13 点分别执行一次任务
->weekly();                     每周执行一次任务
->weeklyOn(1, '8:00');          每周一的 8 点执行一次任务
->monthly();                    每月执行一次任务
->monthlyOn(4, '15:00');        每月 4 号的 15 点执行一次任务
->quarterly();                  每季度执行一次任务
->yearly();                     每年执行一次任务
->timezone('America/New_York'); 设定时区

完成。

相关文章

  • laravel 设置定时任务(任务调度)

    1)创建数据表 2)定义调度:在App\Console\Commands下创建Test.php 3)编辑 app/...

  • laravel 设置定时任务(任务调度)

    创建定时任务 查看定时任务 定义调度 在App\Console\Commands下创建Test.php 编辑 ap...

  • 学习Linux2

    linux 学习2 定时任务管理 crond 任务调度 crontab 进行 定时任务的设置 概述任务调度:是指系...

  • laravel任务调度-定时任务

    写在前面的话: 其实laravel的任务调度(定时任务)在文档中已经写得很清楚了,只是对于刚入门或者还不是很熟练的...

  • Linux任务调度

    crond任务调度 crontab用于定时任务的设置。任务调度:是指系统在某个时间执行的特定的命令或程序。任务调度...

  • Laravel 定时任务调度

    定义调度你可以在App\Console\Kernel类的schedule 方法中定义所有调度任务。在开始之前,先看...

  • Linux-7 任务调度

    crond 任务调度 crontab 进行定时任务的设置 概述 任务调度:是指系统在某个时间执行的特定的命令或程序...

  • Java定时任务调度工具详解

    本篇内容:什么是定时任务调度?Java定时任务调度工具详解之 Timer篇Java定时任务调度工具详解之 Quar...

  • laravel 任务调度

    1.在代码里面写调度任务,在Console文件夹下面Kernel.php写调取任务命令 2.在Commands文件...

  • laravel 任务调度

    创建定时任务文件 编辑文件内容 修改Kernel 文件 添加定时任务

网友评论

      本文标题:laravel 设置定时任务(任务调度)

      本文链接:https://www.haomeiwen.com/subject/buskiktx.html