PHP 3 ways to perform tasks in a timely manner _php skills

Source: Internet
Author: User
Tags curl php script time limit

Three ways to implement PHP at timed intervals

    • 1. Windows Scheduled Tasks
    • 2, the Linux script program
    • 3, let the Web browser refresh regularly

Concrete implementation
1. Windows Planning Tasks

PHP rarely run on the win server, the implementation is no longer detailed, look at the online implementation of the principle is probably to write a bat script, and then let the window task to add execute this bat script.
2, the Linux script implementation
This is mainly used to crontab this command,
How to use:

Copy Code code as follows:
Crontab Filecrontab [-u user] [-u user] {-l |-r | e}

Description

Crontab is used to enable the user to execute the program at a fixed time or at a fixed interval.

Use Crontab to write the shell script, and then let PHP call the shell, this is the use of Linux features, should not be the language of PHP's own features

3, PHP implementation of scheduled Tasks
using PHP to refresh your browser requires several issues
PHP script Execution time limit, the default is 30m solution: Set_time_limit () or modify php.ini set Max_execution_time time (not recommended)
If the client browser is shut down, the program may be forced to terminate, workaround: Ignore_user_abort Even if the shutdown page is still running normally
If the program has been executing very likely to consume a lot of resources, the solution uses sleep to use the program to hibernate for a while and then in the execution
PHP timed Execution Code:

<?php
Ignore_user_abort ()//Turn off the browser, the PHP script can also continue to execute.
Set_time_limit (3000);//Through Set_time_limit (0) allows the program to execute indefinitely
$interval =5;//every 5s run
 
//Method 1--dead loop
do{
  echo ' Test '. Time (). ' <br/> '; 
  Sleep ($interval);//wait 5s  
}while (true)
 
; Method 2---Sleep timed execution
  require_once './curlclass.php '//Introducing file
   
  $curl = new Httpcurl ()//instantiating
  $stime = $curl- >getmicrotime ();
  For ($i =0 $i <=10; $i + +) {
     
    echo ' Test '. Time (). ' <br/> '; 
    Sleep ($interval);//wait for 5s
     
  }
  Ob_flush ();
  Flush ();
  $etime = $curl->getmicrotime ();
  Echo '  
 

This is not very efficient when tested.

Summarize:

Personally feel that PHP time to perform the task is not very efficient, it is recommended that the task of regular execution or to the shell to do it.

The above is PHP time to perform the task of 3 methods, for the previous article at the end of the crontab This command, also carried a brief introduction, I hope we can have some harvest.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.