美文网首页
Laravel6.X定时执行任务

Laravel6.X定时执行任务

作者: 前端组件库 | 来源:发表于2020-02-14 16:15 被阅读0次

定时执行任务其实非常简单,我们都知道php artisan命令会执行某个或多个任务,我们也可以自己定制任务,然后定时运行就可以了。所以定时执行任务我们分为2步,第一步,创建自定义php artisan命令,第二步,在console里面执行php artisan schedule:run,这样我们就可以一直监听并执行定时任务。

1.创建自定义定时执行任务

先执行命令:

php artisan make:command SendEmails

我们会看见在app/console/commands/SendEmails.php生成的这个文件:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

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

    /**
     * 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.
     *
     * @return mixed
     */
    public function handle()
    {
        //
    }
}

在这里我们做几件事:1.定义signature 2.填写description 3.在handle()方法里面填入想要操作的具体逻辑。所以我们可以改成:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'this will send an email';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Mail::send();
        //假的,你要写你自己的命令
        echo "执行完之后返回的文字";
    }
}

我们可以直接执行看下效果:

php artisan send:email

2.启动定时执行

在app/console/Kernel.php文件的schedule方法里添加你的命令,添加两个信息,第一个是命令,第二个是频率。laravel有很多频率的方法,可以参考laravel文档。

<?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('inspire')
        //          ->hourly();

        $schedule->command("send:email")
            ->everyMinute();

    }

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

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

3.怎样让他一直监听执行呢?

php artisan schedule:run

这个命令可以执行您定义的所有定时的php artisan命令,我们可以直接加一次便足够运行所有的定时任务。找到你服务器执行定时任务的文件,添加下一行:

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

相关文章

  • Laravel6.X定时执行任务

    定时执行任务其实非常简单,我们都知道php artisan命令会执行某个或多个任务,我们也可以自己定制任务,然后定...

  • 31-Timer的缺陷分析

    Timer的缺陷分析 Timer计时器可以定时(指定时间执行任务)、延迟(延迟5秒执行任务)、周期性地执行任务(每...

  • Timer定时器

    主要两个要点: 实现指定时间执行任务 实现按照指定周期执行任务 定时器Timer的使用 JDK中Timer类主要负...

  • Quartx 定时执行任务的简单使用

    定时执行任务一般可以使用新建一个线程,让循环执行,这里就介绍下使用Quartx来进行定时执行任务 Maven引入 ...

  • ExecutorService_ScheduledThreadP

    ScheduledThreadPool可以定时的或延时的执行任务

  • crontab定时执行任务

    crontab命令可以设置定时执行任务,比如定时执行备份,定时爬取数据等 我们需要每个小时执行一次爬虫,cront...

  • 【python】定时执行任务

    schedule 官网示例: 本地实例:

  • macos定时执行任务

    macos系统自带的守护进程launchctl既可以执行定时任务 1、进入~/Library/LaunchAgen...

  • Java定时执行任务

    1.建立TimerManage类,设置时间点,时间点设置的管理类,代码如下: 在 TimerManager 这个类...

  • Ubuntu定时执行任务

    ubuntu定时执行任务 cron是一个Linux下的后台进程,用来定期的执行一些任务。因为我用的是Ubuntu,...

网友评论

      本文标题:Laravel6.X定时执行任务

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