Home>Article>PHP Framework> Detailed explanation of how thinkphp5.1 implements scheduled tasks

Detailed explanation of how thinkphp5.1 implements scheduled tasks

藏色散人
藏色散人 forward
2021-06-09 15:19:10 5100browse

The following tutorial column ofthinkphp frameworkwill explain in detail how thinkphp5.1 implements scheduled tasks. I hope it will be helpful to friends in need!

Preface

My friends and I wrote a small system for micro-classroom using thinkphp5.1. Implement a daily class reminder and data initialization function. The method is now written, but each execution requires a manual click on the administrator interface. It feels very troublesome. How can I make it automatically execute at a scheduled time every day?
Detailed explanation of how thinkphp5.1 implements scheduled tasks

Predecessor methods

Search tp5.1 timed tasks or planned tasks on Google. There will be many blogs, but most of them are scripts under Linux. I just Install the Linux system. I am not very familiar with Linux, so I will first use the Windows system.

Idea

The implementation ideas in windows environment and Linux environment are the same.
1. Now write the implementation method into a trigger, and you can click to jump to implement it.
2. Write a script to automatically open a web page.
3. Use the scheduled task that comes with Windows to automatically execute this script at regular intervals.

Implementation

1. We now write a test trigger under the login controller

public function test() // 自动执行测试 { echo "这是一个定时任务的测试"; }

The trigger effect is as follows
Detailed explanation of how thinkphp5.1 implements scheduled tasks

##2 .Write a script in .bat format to automatically open this web page.

Create another .txt file and write the execution success information.


Detailed explanation of how thinkphp5.1 implements scheduled tasks

The code is as follows

@echo off start iexplore.exe http://127.0.0.1/index/login/test Ping -n 5 127.1>nul Taskkill /f /im iexplore.exe echo success >> test.txt

start iexplore.exe http://127.0.0.1/index/login/testrepresents the URL to open the browser The specific URL is written at the end,Ping -n 5 127.1>nulmeans waiting for 5 seconds,Taskkill /f /im iexplore.exemeans closing the browser,echo success > ;> test.txtindicates a successful return to Notepad. Since writing comments will cause an error in the code, write a code comment here.Click on the test .bat file

Detailed explanation of how thinkphp5.1 implements scheduled tasks

The effect of Notepad is as shown below


Detailed explanation of how thinkphp5.1 implements scheduled tasks

You can also change the code written into Notepad to DingTalk Reminder

The code is as follows

curl -X POST -H "Content-type: application/json" ^ --data "{\"text\": {\"content\": \"每日初始完成\"}, \"msgtype\": \"text\"}" ^ https://oapi.dingtalk.com/robot/send?access_token=23dc64f0dee37b5ee3b1ac472b2c84d837afa15b2a884b87b32f898998408a36
Since the function of adding DingTalk robots is temporarily closed, it will not be demonstrated here.

3. Use Windows’ built-in scheduled tasks to implement scheduled self-starting scripts

Click on the control panel, click Management Tools->Task Scheduler->Create basic tasks


Detailed explanation of how thinkphp5.1 implements scheduled tasks

Name: Daily Initial

Trigger: Every day
Start: //Choose a time you like
Operation: Start the program->Select this script
Complete

Detailed explanation of how thinkphp5.1 implements scheduled tasks

It will automatically start at the set time.

Conclusion

The specific operation methods of Linux systems are different, but the ideas are the same. I will provide a link to a blog I saw below. After I understand the Linux system, I can use Linux system implementation.

Related recommendations:

The latest 10 thinkphp video tutorials

The above is the detailed content of Detailed explanation of how thinkphp5.1 implements scheduled tasks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete