一,創建命令
版本<5.3
Php artisan make:console command_name --command=artisan_command_name
版本>=5.3
Php artisan make:command command_name --command=artisan_command_name
command_name:生成的文件名
artisan_command_name: php artisan命令調度時的命令名稱
結果: 在App\Console\Commands\ 下生成名為command_name.php的文件.
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
Tip:該命令只執行一次!
b.設定定時任務
需求:有的業務需求要求在某個特定的時間段里執行某種任務,所以就用到了定時任務
輸入命令
crontab -e
編寫以下cron語句:
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
php和artisan都要寫完整的路徑
如果你不知道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
刪除所有定時任務: crontab -r
crontab -r
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
文件/etc/crontab中每行任務的描述格式如下:
```
minute - 從0到59的整數
hour - 從0到23的整數
day - 從1到31的整數 (必須是指定月份的有效日期)
month - 從1到12的整數 (或如Jan或Feb簡寫的月份)
dayofweek - 從0到7的整數,0或7用來描述周日 (或用Sun或Mon簡寫來表示)
command - 需要執行的命令(可用as ls /proc >> /tmp/proc或 執行自定義腳本的命令)
```
```
分鍾 (0-59)
```
上面舉例了兩種實現方法,方法一是用閉包,方法二是用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
當你不想在定時任務里執行的時候
1,schedule里注釋掉你不想執行的任務
2,在crontab里用#好注釋掉定時任務
具體情況酌情處理