search
HomePHP FrameworkThinkPHPThinkPHP implementation of scheduled tasks case

ThinkPHP implementation of scheduled tasks case

Mar 11, 2020 am 09:50 AM
thinkphpscheduled tasks

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)

<?php  
  
return array(  
    //&#39;配置项&#39;=>&#39;配置值&#39;  
    &#39;app_begin&#39; =>array(&#39;Behavior\CronRunBehavior&#39;),  
);

②, 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.)

<?php  
  
return array(  
    //myplan为我们计划定时执行的方法文件,2是间隔时间,nextruntime下次执行时间  
    //此文件位于/Application/Cron/目录下  
    &#39;cron&#39; => array(&#39;myplan&#39;, 2, nextruntime),  
);

③, myplan.php

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

<?php  
  
echo date("Y-m-d H:i:s")."执行定时任务!" . "\r\n<br>";

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:

<?php
return array (
  &#39;cron&#39; => 
  array (
    0 => &#39;myplan&#39;,
    1 => 2,
    2 => 1502089802,
  ),
);
?>

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

②, edit test.php

<?php
  $txt = "/data/wwwroot/door/test.txt";
  // die(var_dump($txt));
  $date=date(&#39;Y-m-d H:i:s&#39;,time());
  $content = file_get_contents($txt);
  if($content!=&#39;&#39;){
    $arr=explode(&#39;#&#39;,$content);
    $num=$arr[&#39;1&#39;]+1;
    $string=$date.&#39;#&#39;.$num;
  }else{
    $string=$date.&#39;#&#39;.&#39;1&#39;;
  }
  file_put_contents($txt,$string);
  $content_last = file_get_contents($txt);
  return $content_last;

③、Backend monitoring test.txt file

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

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!

Statement
This article is reproduced at:www.100txy.com. If there is any infringement, please contact admin@php.cn delete
What Are the Key Features of ThinkPHP's Built-in Testing Framework?What Are the Key Features of ThinkPHP's Built-in Testing Framework?Mar 18, 2025 pm 05:01 PM

The article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.

How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?Mar 18, 2025 pm 04:57 PM

Article discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.

What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?Mar 18, 2025 pm 04:54 PM

The article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges

How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?Mar 18, 2025 pm 04:51 PM

The article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]

What Are the Advanced Features of ThinkPHP's Dependency Injection Container?What Are the Advanced Features of ThinkPHP's Dependency Injection Container?Mar 18, 2025 pm 04:50 PM

ThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159

How to Use ThinkPHP for Building Real-Time Collaboration Tools?How to Use ThinkPHP for Building Real-Time Collaboration Tools?Mar 18, 2025 pm 04:49 PM

The article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.

What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?Mar 18, 2025 pm 04:46 PM

ThinkPHP benefits SaaS apps with its lightweight design, MVC architecture, and extensibility. It enhances scalability, speeds development, and improves security through various features.

How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?Mar 18, 2025 pm 04:45 PM

The article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.