ThinkPHP5實現定時任務


最近使用ThinkPHP5做了個項目,項目中需要定時任務的功能,感覺有必要分享下 
TP5做定時任務使用到command.php的 
步驟如下: 
1.配置command.php文件,位於application/command.php 

<?php namespace app\home\command; use think\console\Command; use think\console\Input; use think\console\Output; class Test extends Command { protected function configure() { $this->setName('test')->setDescription('Here is the remark '); } protected function execute(Input $input, Output $output) { $schedule = new Scheduletask(); $schedule -> run(); $output->writeln("TestCommand:"); } }

 

configure函數是在命令行中用list命令列出所有任務的時候回顯示的出的提示,execute函數是說要執行的命令,在這里可以直接調用其他函數,完成例如統計等任務工作,然后用output輸出到命令行 
3.運行test命令 
打開命令行,運行php think test命令test命令execute方法中運行的方法就會運行 
4.在入口文件處,新建bat文件,內容如下 
D: 
cd D:\phpStudy\WWW\OilTycoon 
php think test 
切換到D盤 
切換到項目根目錄處 
運行test命令 
5.把bat文件加入到服務器的定時任務中


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM