詳解thinkphp5.1/5.0定時任務的實現步驟

下面由thinkphp教程欄目給大家詳解thinkphp5.1/5.0定時任務的實現步驟,希望對需要的朋友有所幫助!

我主要做的是一個員工生日當天發短信的功能,每天跑一次腳本,

第一步:

a.App/模塊/ 下創建command文件夾

b.我這邊是創建在admin模塊里面,在command文件夾下創建一個SendMessage.php文件(具體名字自己根據需求定)

c.復制下面的代碼到SendMessage.php

立即學習PHP免費學習筆記(深入)”;

<?php  namespace appadmincommand;     use thinkconsoleCommand;  use thinkconsoleInput;  use thinkconsoleOutput;  use thinkDb;  use thinkLog;     class SendMessage extends Command  {      protected function configure(){          $this->setName('SendMessage')-&gt;setDescription("計劃任務?SendMessage"); ?????} ?? ?????//調用SendMessage?這個類時,會自動運行execute方法 ?????protected?function?execute(Input?$input,?Output?$output){ ?????????$output-&gt;writeln('Date?Crontab?job?start...'); ?????????/***?這里寫計劃任務列表集?START?***/ ?? ?????????$this-&gt;birthday();//發短信 ?? ?????????/***?這里寫計劃任務列表集?END?***/ ?????????$output-&gt;writeln('Date?Crontab?job?end...'); ?????} ?? ?????//獲取當天生日的員工?發短信 ?????public?function?birthday() ?????{ ?????????echo?'這里寫你要實現的邏輯代碼'; ?????} ?}

第二步:在APP/command.php里面加上

return?['appadmincommandSendMessage'];

詳解thinkphp5.1/5.0定時任務的實現步驟

第三步:設置crontab計劃任務

  1. crontab -l //計劃任務列表

  2. crontab -e //編輯新增

  3. crontab -r //刪除

為了方便測試,可以先設置成每分鐘執行一次?,記錄一下日志/www/wwwroot/tool/runtime/message/2019.log

  1. */1 * * * * php /www/wwwroot/tool/think SendMessage>>/www/wwwroot/tool/runtime/message/2019.log 2>&1

  2. ?
  3. //監控一下你的腳本是不是正常的

  4. tail -f?/www/wwwroot/tool/runtime/message/2019.log

? 版權聲明
THE END
喜歡就支持一下吧
點贊5 分享