欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學堂PHP培訓簡述如何實現(xiàn)Laravel框架定時任務

2019-04-03 15:08:53 2244瀏覽

Laravel框架定時任務有多少的小伙伴知道呢?本篇文章扣丁學堂PHP培訓小編給大家分享Laravel框架定時任務2種實現(xiàn)方式,文中有詳細的代碼列出供大家參考,希望對小伙伴們有所幫助。



PHP培訓



第一


1、生成一個commands文件


> php artisan make:command test


2、打開文件進行修改


laravel\App\Console\Commands\test.php


<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class test extends Command
{
 /**
  * The name and signature of the console command.
  *
  * @var string
  */
 protected $signature = 'test:insert'; // php artisan list 中將會生成 "php artisan test:insert " 指令
 /**
  * The console command description.
  *
  * @var string
  */
 protected $description = 'insert Test table some test data'; // 對上面指令的描述
 /**
  * Create a new command instance.
  *
  * @return void
  */
 public function __construct()
 {
  parent::__construct();
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
  // 編寫你要的定時任務執(zhí)行的代碼!
  # eg
 Log::info('test');
 }
}

> php artisan list 查看


Python視頻


3、然后修改: laravel\app\Console\Kernel.php 文件


<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
 protected $commands = [
  // 參考手冊 新加
  \App\Console\Commands\test::class,
 ];
 // 定義應用的命令調度
 protected function schedule(Schedule $schedule)
 {
   // 新加 每分鐘執(zhí)行一次
  $schedule->command('test:insert')->everyMinute();
 }
 protected function commands()
 {
  $this->load(__DIR__.'/Commands');
  require base_path('routes/console.php');
 }
}


4、啟用計劃任務:在服務器中加入到計劃任務 crontab -e


注意這里的 path 是你的laravel項目根目錄的 絕對路徑!, 然后加上后面的 artisan 到結尾的字符串


* * * * * php /path/artisan schedule:run >> /dev/null 2>&1

* * * * * php /code/src/laravel/artisan schedule:run >> /dev/null 2>&1


5、打開日志文件查看


laravel\storage\logs\laravel.log



二種


使用 shell腳本執(zhí)行


因為 php artisan list 可以查看到 執(zhí)行指令 test:insert所以可以考慮用 .sh 腳本執(zhí)行,還是類似上面 crontab -e編寫


1、先編寫 .sh 腳本 laravel/test.sh 放在項目某個位置,文件內寫入


php artisan test:insert


上面指令在命令行手動每執(zhí)行一次就可以觸發(fā)一次編寫的程序,相當于給 laravel.log 寫入一次 test


2、使用 crontab -e 編寫 執(zhí)行 第一步寫的 test.sh 腳本


* * * * * laravel/test.sh


以上兩種均可看到 laravel.log 日志


Python學習


想要了解更多關于PHP開發(fā)方面內容的小伙伴,請關注扣丁學堂PHP培訓官網、微信等平臺,扣丁學堂IT職業(yè)在線學習教育有專業(yè)的PHP講師為您指導,此外扣丁學堂老師精心推出的PHP視頻教程定能讓你快速掌握PHP從入門到精通開發(fā)實戰(zhàn)技能??鄱W堂PHP技術交流群:374332265。


扣丁學堂微信公眾號                          Python全棧開發(fā)爬蟲人工智能機器學習數(shù)據分析免費公開課直播間


【關注微信公眾號獲取更多學習資料】         【掃碼進入Python全棧開發(fā)免費公開課】



查看更多關于“php培訓資訊”的相關文章>>



標簽: PHP培訓 PHP基礎教程 PHP學習視頻 PHP教學視頻 PHP入門教程 PHP教程視頻 PHP在線學習 PHP在線視頻 PHP在線教程 扣丁學堂PHP培訓

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部