Home >Backend Development >PHP Tutorial >PHP scheduled execution method

PHP scheduled execution method

墨辰丷
墨辰丷Original
2018-06-05 16:53:322497browse

This article mainly introduces the method of PHP timing execution. Interested friends can refer to it. I hope it will be helpful to everyone.

Three ways to implement PHP scheduled execution

  • 1. Windows scheduled tasks

  • 2. Linux script

  • 3. Let the web browser refresh regularly

Specific implementation
1.Windows scheduled tasks

PHP rarely runs on win servers, and the specific implementation will not be studied in detail. The principle of online implementation is probably to write a bat script, and then let the window task add and execute the bat script.
2. Linux script implementationThe crontab command is mainly used here,
Usage method:

The code is as follows:

crontab filecrontab [-u user ] [ -u user ] { -l | -r | -e }

Explanation:

crontab is used to allow users to For executing programs at a fixed time or at fixed intervals

Use crontab to write a shell script, and then let PHP call the shell. This is using the characteristics of Linux and should not be considered the characteristics of PHP's own language

3. PHP implements scheduled execution of scheduled tasks Using PHP to refresh the browser requires solving several problems
PHP script execution time limit, the default is 30m Solution: set_time_limit(); or modify PHP.ini settings max_execution_time time (not recommended)
If the client browser is closed, the program may be forced to terminate. Solution: ignore_user_abort will still execute normally even if the page is closed.
If the program continues to execute, it is likely to consume a lot of resources. Solution The method is to use sleep to sleep for a while, and then execute the
PHP scheduled execution code:

<?php
ignore_user_abort();//关掉浏览器,PHP脚本也可以继续执行.
set_time_limit(3000);// 通过set_time_limit(0)可以让程序无限制的执行下去
$interval=5;// 每隔5s运行
 
//方法1--死循环
do{
  echo &#39;测试&#39;.time().&#39;<br/>&#39;; 
  sleep($interval);// 等待5s  
}while(true);
 
//方法2---sleep 定时执行
  require_once &#39;./curlClass.php&#39;;//引入文件
   
  $curl = new httpCurl();//实例化
  $stime = $curl->getmicrotime();
  for($i=0;$i<=10;$i++){
     
    echo &#39;测试&#39;.time().&#39;<br/>&#39;; 
    sleep($interval);// 等待5s
     
  }
  ob_flush();
  flush();
  $etime = $curl->getmicrotime();
  echo &#39;<hr>&#39;;
  echo round(($etime-stime),4);//程序执行时间

When testing, we found that this efficiency is not very high.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Detailed explanation of examples returned by PHP implementation function reference

PHP introductory tutorial image processing skills example sharing

PHP Introductory Tutorial Detailed Examples of Basic Object-Oriented Concepts

The above is the detailed content of PHP scheduled execution method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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