The tutorial column of thinkphp below will give you a detailed explanation of the steps to implement thinkphp5.1/5.0 scheduled tasks. I hope it will be helpful to friends in need!
What I mainly do is the function of sending text messages to employees on their birthdays. I run the script once a day.
The first step:
a.App Create a command folder under /module/
b. I created it in the admin module and created a SendMessage.php file under the command folder (the specific name is determined by yourself according to your needs)
c. Copy the following code to SendMessage.php
<?php namespace app\admin\command; use think\console\Command; use think\console\Input; use think\console\Output; use think\Db; use think\Log; class SendMessage extends Command { protected function configure(){ $this->setName('SendMessage')->setDescription("计划任务 SendMessage"); } //调用SendMessage 这个类时,会自动运行execute方法 protected function execute(Input $input, Output $output){ $output->writeln('Date Crontab job start...'); /*** 这里写计划任务列表集 START ***/ $this->birthday();//发短信 /*** 这里写计划任务列表集 END ***/ $output->writeln('Date Crontab job end...'); } //获取当天生日的员工 发短信 public function birthday() { echo '这里写你要实现的逻辑代码'; } }
Step 2: Add
return ['app\admin\command\SendMessage'];
Step 3: Set up crontab scheduled tasks
##crontab -l //Scheduled task list
crontab -e //Edit to add
crontab -r //Delete
*/1 * * * * php /www/wwwroot/tool/think SendMessage>>/www/wwwroot/tool/runtime/message/2019.log 2>&1
The above is the detailed content of Detailed explanation of the implementation steps of thinkphp5.1/5.0 scheduled tasks. For more information, please follow other related articles on the PHP Chinese website!