最近使用ThinkPHP5作了個項目,項目中須要定時任務的功能,感受有必要分享下 html
TP5作定時任務使用到command.php的 linux
步驟以下: windows
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) { echo '已經開始了'; $output->writeln("TestCommand:"); } }app
configure函數是在命令行中用list命令列出全部任務的時候回顯示的出的提示,execute函數是說要執行的命令,在這裏能夠直接調用其餘函數,完成例如統計等任務工做,而後用output輸出到命令行 ide
3.運行test命令 函數
打開命令行,運行php think test命令test命令execute方法中運行的方法就會運行 this
4.在入口文件處,新建bat文件,內容以下 spa
D:
cd D:\phpStudy\WWW\OilTycoon
php think test
切換到D盤
切換到項目根目錄處
運行test命令
5.把bat文件加入到服務器的定時任務中
1、windows:
// 這裏是跳轉到項目目錄
cd D:\WWW\ForeManApi php think spider
2、linux:
// 爲think添加執行權限chmod +x /home/wwwroot/default/www.xxx.com/think crontab -e // 每一小時執行一次 0 */1 * * * /usr/local/php/bin/php /home/wwwroot/default/www.xxx.com/think spider