Home> PHP Framework> ThinkPHP> body text

ThinkPHP implementation of scheduled tasks case

angryTom
Release: 2020-03-11 09:50:33
forward
11360 people have browsed it

This article introduces the method of using ThinkPHP to implement scheduled tasks, and the method of using cron to implement scheduled tasks. I hope it will be helpful to friends who are learning thinkphp!

ThinkPHP implementation of scheduled tasks case

ThinkPHP implementation of scheduled task case

The most common scheduled task is the crontab scheduled task in Linux. This is executed by writing a script, and it will be executed in a loop in the background. But sometimes we don’t have server permissions or we don’t have an independent server, so what should we do? In fact, there is another type of scheduled tasks that is passive, which will be triggered as long as the project is accessed. Passive scheduled tasks are generally used for virtual hosts, because without server permissions we can only implement them through code. Let's take thinkPHP as an example to analyze the difference between these two scheduled tasks.

(Recommended tutorial:thinkphp tutorial)

Passive scheduled tasks

①、tags.php

Create a new tags.php file in the /Application/Common/Conf directory. (This is the same as method 1)

'配置值' 'app_begin' =>array('Behavior\CronRunBehavior'), );
Copy after login

②, crons.php

Create a new crons.php file in the /Application/Common/Conf directory. (This is different from method 1, please pay attention to the distinction.)

 array('myplan', 2, nextruntime), );
Copy after login

③, myplan.php

Create a new Cron folder in the /Application/Common/ directory, and create a new file myplan.php in it document.

";
Copy after login

At this point we can access the url of the project, and then we will find that the ~crons.php file is generated in the Application/Runtime/ directory. At the same time, the page will have the following effect, and the file content is as follows:

 array ( 0 => 'myplan', 1 => 2, 2 => 1502089802, ), ); ?>
Copy after login

ThinkPHP implementation of scheduled tasks case

Active scheduled tasks

①, log in to the Linux server

[root@iZwz924w5t4862mn4tgcyqZ ~]# crontab -e */1 * * * * /usr/local/php/bin/php /data/wwwroot/door/test.php//执行PHP文件 */1 * * * * /usr/bin/curl http://www.100txy.com/wechatapi.php//访问url
Copy after login

②, edit test.php


        
Copy after login

③、Backend monitoring test.txt file

[root@iZwz924w5t4862mn4tgcyqZ ~]# tail -f /data/wwwroot/door/test.txt
Copy after login

ThinkPHP implementation of scheduled tasks case

(Free learning video tutorial sharing:php video tutorial)

The above is the detailed content of ThinkPHP implementation of scheduled tasks case. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:www.100txy.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!