Laravel框架定時(shí)任務(wù)2種實(shí)現(xiàn)方式示例
本文實(shí)例講述了Laravel框架定時(shí)任務(wù)2種實(shí)現(xiàn)方式。分享給大家供大家參考,具體如下:
第一種
1、生成一個(gè)commands文件
> php artisan make:command test
2、打開文件進(jìn)行修改
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 中將會(huì)生成 "php artisan test:insert " 指令 /** * The console command description. * * @var string */ protected $description = 'insert Test table some test data'; // 對(duì)上面指令的描述 /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { // 編寫你要的定時(shí)任務(wù)執(zhí)行的代碼! # eg Log::info('test'); } }
> php artisan list
查看
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 = [ // 參考手冊(cè) 新加 \App\Console\Commands\test::class, ]; // 定義應(yīng)用的命令調(diào)度 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、啟用計(jì)劃任務(wù):在服務(wù)器中加入到計(jì)劃任務(wù) crontab -e
注意這里的 path 是你的laravel項(xiàng)目根目錄的 絕對(duì)路徑!, 然后加上后面的 artisan 到結(jié)尾的字符串
* * * * * 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í)行
因?yàn)?php artisan list
可以查看到 執(zhí)行指令 test:insert
所以可以考慮用 .sh 腳本執(zhí)行,還是類似上面 crontab -e
編寫
1、先編寫 .sh 腳本 laravel/test.sh 放在項(xiàng)目某個(gè)位置,文件內(nèi)寫入
php artisan test:insert
上面指令在命令行手動(dòng)每執(zhí)行一次就可以觸發(fā)一次編寫的程序,相當(dāng)于給 laravel.log 寫入一次 test
2、使用 crontab -e
編寫 執(zhí)行 第一步寫的 test.sh 腳本
* * * * * laravel/test.sh
以上兩種均可看到 laravel.log 日志
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
- Laravel實(shí)現(xiàn)定時(shí)任務(wù)的示例代碼
- Laravel框架實(shí)現(xiàn)定時(shí)發(fā)布任務(wù)的方法
- 源碼分析 Laravel 重復(fù)執(zhí)行同一個(gè)隊(duì)列任務(wù)的原因
- Laravel中任務(wù)調(diào)度console使用方法小結(jié)
- Laravel框架數(shù)據(jù)庫(kù)CURD操作、連貫操作總結(jié)
- Laravel框架表單驗(yàn)證詳解
- Laravel框架中擴(kuò)展函數(shù)、擴(kuò)展自定義類的方法
- 跟我學(xué)Laravel之快速入門
- Laravel框架路由配置總結(jié)、設(shè)置技巧大全
- Laravel中使用FormRequest進(jìn)行表單驗(yàn)證方法及問題匯總
相關(guān)文章
創(chuàng)建配置文件 用PHP寫出自己的BLOG系統(tǒng) 2
今天做博客安裝程序,首先做的是配置文件的創(chuàng)建。2010-04-04PHP包含文件函數(shù)include、include_once、require、require_once區(qū)別總結(jié)
include() 、require()語句包含并運(yùn)行指定文件。這兩結(jié)構(gòu)在包含文件上完全一樣,唯一的區(qū)別是對(duì)于錯(cuò)誤的處理。require()語句在遇到包含文件不存在,或是出錯(cuò)的時(shí)候,就停止即行,并報(bào)錯(cuò)。include()則繼續(xù)即行。2014-04-04Laravel 5.4因特殊字段太長(zhǎng)導(dǎo)致migrations報(bào)錯(cuò)的解決
這篇文章主要給大家介紹了關(guān)于Laravel 5.4因特殊字段太長(zhǎng)導(dǎo)致migrations報(bào)錯(cuò)的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起看看吧。2017-10-10PHP receiveMail實(shí)現(xiàn)收郵件功能
這篇文章主要為大家詳細(xì)介紹了PHP receiveMail實(shí)現(xiàn)收郵件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04PHP中用mysqli面向?qū)ο蟠蜷_連接關(guān)閉mysql數(shù)據(jù)庫(kù)的方法
下面小編就為大家?guī)硪黄狿HP中用mysqli面向?qū)ο蟠蜷_連接關(guān)閉mysql數(shù)據(jù)庫(kù)的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11C#使用PHP服務(wù)端的Web Service通信實(shí)例
這篇文章主要介紹了C#使用PHP服務(wù)端的Web Service通信實(shí)例,需要的朋友可以參考下2014-04-04