laravel 定时任务

我也是在这之前没有接触过laravel的任务调度,参考的是这篇laravel定时任务,然后自己总结来写篇了这文章,方便自己以后使用。

laravel框架是自带有个任务调度的。中文手册laravel5.4任务调度

看文档


按照文档流程,第一步先去服务器添加一条定时任务,这里你需要先看看自己php配置全局变量没有,在服务器命令行输入

php -v

随便在服务器什么位置都行,如果没有报错,就证明你配置了php全局变量。没有配置的建议去百度下如何配置

然后在服务器写入这个定时任务

* * * * * /usr/bin/php /var/www/test/artisan schedule:run >> /dev/null 2>&1

上面这里/usr/bin/php是php的路径,可以使用命令行

whereis php
或者
which php

来查看,/var/www/test/artisan是项目地址根目录下的artisan文件的绝对路径,接着用使用命令创建

php artisan make:console TestConsole
使用如上命令可能会出现下面报错
Command "make:console" is not defined.  
                                          
  Did you mean one of these?              
      make:auth                           
      make:command                        
      make:controller                     
      make:event                          
      make:job                            
      make:listener                       
      make:mail                           
      make:middleware                     
      make:migration                      
      make:model                          
      make:notification                   
      make:policy                         
      make:provider                       
      make:request                        
      make:seeder                         
      make:test        

把make:console改成make:command即可,会在app\Console下生成一个Commands文件夹,同时生成TestConsole.php,以下就是TestConsole.php的内容

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class TestConsole 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()
    {
        //
    }
}

让我们来看看这个文件,protected $signature = 'command:name';上面注释说的很明白,这里改成当前文件的名称就好了;

protected $description = 'Command description';这是改成说明描述,

public function handle()主要就是写业务功能;

在TestConsole.php加上use Illuminate\Support\Facades\Log;

在public function handle()写入Log::info('laravel定时任务'.time());

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

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

    /**
     * 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()
    {
        //
		Log::info('laravel定时任务'.time());
    }
}

然后去app\Console下的Kernel.php,我们开打来看看

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

    /**
     * Register the Closure based commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        require base_path('routes/console.php');
    }
}

 看这个文件,protected $commands = [  ];这里就写刚刚我们创建的TestConsole.php文档的地址,

\App\Console\Commands\TestConsole::class或者Commands\TestConsole::class。

protected function schedule(Schedule $schedule)就是写任务调度了。在里面写上$schedule->command('testconsole')->everyMinute();意思就是每一分钟运行一次TestConsole.php。

<?php

namespace App\Console;

//use DB;
use Illuminate\Support\Facades\DB;
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\TestConsole::class,
		//Commands\TestConsole::class
    ];

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

    /**
     * Register the Closure based commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        require base_path('routes/console.php');
    }
}

这时候我们就可以去看我们的定时任务成功没有,来到storage\logs\laravel.log,就可以查看有没有我们在TestConsole.php文件写入的Log::info('laravel定时任务'.time());如果是用服务器直接查看的小伙伴就不要用cat命令了,可能laravel.log行数很多,我们查看最后几行就好了,使用命令tail命令查看最后20行

tail -n 20 storage/logs/laravel.log

这时候就能发现我们的laravel的任务调度成功调用。


猜你喜欢

转载自blog.csdn.net/qq_38375394/article/details/79711975