官方文檔給出的教程已經很詳細了,這里給出一些補充幫助大家理解。
英文文檔:https://laravel.com/docs/5.2/scheduling
中文文檔:https://laravel-china.org/docs/5.2/scheduling
Starting The Scheduler
這里文檔說的很簡單,就是讓你在服務器的crontab加入一條命令。
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
關於crontab可以參考這篇文章:http://www.cnblogs.com/xxoome/p/6091459.html
這條命令什么意思呢?按照crontab配置文件格式的解釋
紅框內都是shell命令。
##如果配有配置php的全局環境變量,這里需要指定php的絕對路徑。
php:/usr/local/php/bin/php
##就是你項目根目錄下的artisan文件的絕對路徑
artisan:/home/prj-test/test/artisan
例如:
* * * * * /usr/local/php/bin/php /home/prj-test/test/artisan schedule:run >> /dev/null 2>&1
========================== 我是分割線 ===========================
1、創建artisan命令行
文檔地址:https://laravel-china.org/docs/5.2/artisan
php artisan make:console TestConsole
<?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 = '這是一個測試artisan的描述'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { Log::info('這是我寫的log'); } }
2、編寫Kernel
<?php namespace App\Console; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; use Illuminate\Support\Facades\Log; class Kernel extends ConsoleKernel { /** * The Artisan commands provided by your application. * * @var array */ protected $commands = [ // Commands\Inspire::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('inspire') // ->hourly(); Log::info('zddddd'); //調用artisan $schedule->command('testconsole')->everyMinute(); } }
有問題歡迎留言交流。
技術交流群:576269252
--------------------------------------
聲明: 原創文章,未經允許,禁止轉載!
--------------------------------------