laravel 定时任务调度

一,创建命令

php artisan make:console Test --command=xzj:test

其中Test是命令名,xzj:test是控制台执行的命令,类似make:console

执行完成后,会在app/Console/Commands目录下生成一个Test.php文件:

 1 <?php
 2 
 3 namespace App\Console\Commands;
 4 
 5 use Illuminate\Console\Command;
 6 use Illuminate\Support\Facades\Log;
 7 
 8 class Test extends Command
 9 {
10     /**
11      * The name and signature of the console command.
12      *用来描述命令的名字与参数
13      * @var string
14      */
15     protected $signature = 'xzj:test';
16 
17     /**
18      * The console command description.
19      *存储命令描述
20      * @var string
21      */
22     protected $description = 'Command 测试';
23 
24     /**
25      * Create a new command instance.
26      *
27      * @return void
28      */
29     public function __construct()
30     {
31         parent::__construct();
32     }
33 
34     /**
35      * Execute the console command.
36      *执行命令
37      * @return mixed
38      */
39     public function handle()
40     {
41         //这里做任务的具体处理,可以用模型
42         Log::info('任务调度二'.date('Y-m-d H:i:s',time()));
43     }
44 }

二,运行命令

在运行命令前需要将其注册到App\Console\Kernel$commands属性中:

 1 <?php
 2 
 3 namespace App\Console;
 4 
 5 use function foo\func;
 6 use Illuminate\Console\Scheduling\Schedule;
 7 use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
 8 use Illuminate\Support\Facades\Log;
 9 
10 class Kernel extends ConsoleKernel
11 {
12     /**
13      * The Artisan commands provided by your application.
14      *定义Artisan命令
15      * @var array
16      */
17     protected $commands = [
18         //
19         Commands\Test::class,
20     ];
21 
22     /**
23      * Define the application's command schedule.
24      *定义调度任务
25      * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
26      * @return void
27      */
28     protected function schedule(Schedule $schedule)
29     {
30         // $schedule->command('inspire')
31         //          ->hourly();
32         //方法一:
33 //        $schedule->call(function (){
34 //           Log::info('任务调度一:闭包形式');
35 //        })->everyMinute();
36         //方法二
37         $schedule->command('xzj:test')->everyMinute();
38     }
39 
40     /**
41      * Register the commands for the application.
42      *
43      * @return void
44      */
45     protected function commands()
46     {
47         $this->load(__DIR__.'/Commands');
48 
49         require base_path('routes/console.php');
50     }
51 }

a.在控制台上执行Artisan命令:

php artisan xzj:test

b.设定定时任务

需求:有的业务需求要求在某个特定的时间段里执行某种任务,所以就用到了定时任务

输入命令

crontab -e

编写以下cron语句:

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

phpartisan都要写完整的路径

如果你不知道PHP的路劲,可以用which php   不要用whereis php 

which php

输出:

/Applications/XAMPP/xamppfiles/bin/php
path-to-your-project    就是你项目的绝对路径:根目录

添加完了之后输入以下命令可以查看:

crontab  -l

显示:

* * * * * /Applications/XAMPP/xamppfiles/bin/php  /Applications/XAMPP/xamppfiles/htdocs/blog/artisan schedul:run >> /dev/null 2>&1

schedule:run 会执行App\Console\Kernel里schedule下的注册命令

如果你想单独执行某个命令可以这样:

例如:你想执行xzj:test命令

* * * * * /Applications/XAMPP/xamppfiles/bin/php  /Applications/XAMPP/xamppfiles/htdocs/blog/artisan xzj:test >> /dev/null 2>&1

上面举例了两种实现方法,方法一是用闭包,方法二是用Artisan命令实现的。 
调度的时间可以有多种: 
->cron(‘* * * * *’); 在自定义Cron调度上运行任务 
->everyMinute(); 每分钟运行一次任务 
->everyFiveMinutes(); 每五分钟运行一次任务 
->everyTenMinutes(); 每十分钟运行一次任务 
->everyThirtyMinutes(); 每三十分钟运行一次任务 
->hourly(); 每小时运行一次任务 
->daily(); 每天凌晨零点运行任务 
->dailyAt(‘13:00’); 每天13:00运行任务 
->twiceDaily(1, 13); 每天1:00 & 13:00运行任务 
->weekly(); 每周运行一次任务 
->monthly(); 每月运行一次任务 

任务钩子:参考下面的链接
调度的时间具体的方法请参考:http://laravelacademy.org/post/9000.html

猜你喜欢

转载自www.cnblogs.com/xzj8023tp/p/9298112.html