How to implement scheduled tasks in PHP, implement tasks in PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:17:48
Original
1101 people have browsed it

PHP implements the method of regularly executing tasks, php implements tasks

The example in this article describes how to implement scheduled tasks in PHP. The code is simple and practical. Share it with everyone for your reference.

The specific implementation method is as follows:

ignore_user_abort(true); //客户端断开时忽略脚本中止(允许脚本一直执行)
set_time_limit(0);  //设置脚本最长执行时间,0不限制
do{
  $handle = fopen('auto.txt', 'w');
  if($fp)
  {
    $text = '你好\n\r';
    $count = 0;
    for($i=1; $i<10; $i++)
    {
      if(! $c = fwrite($handle, '第'.$i.'行:'.$text)) //返回写入字符数,失败时返回false
      {
        echo '第'.$i.'次的写入失败!';
      }
      $count += $c;
    }
  }
  fclose($handle);
  sleep(60); //延缓60秒执行
}while(true);

Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

How to implement scheduled execution of scripts in PHP?

If you want to use PHP files, you can barely do it
There is a sleep function in php. The specific use is sleep(time) where time is in seconds
First, create a php script
while(1){
//yourcode
sleep(3600*24);
}
?>
The yourcode here is the function you want the PHP script to execute. Although this goal can be achieved, there are sacrifices. You have to access this script through the URL at 8 o'clock in the morning, which means executing the PHP file, and this link cannot be interrupted and must continue! ! Otherwise, it will be invalid

PHP is executed only upon request, and is executed only when there is a request.

How to use PHP language to make the system execute a task regularly?

ignore_user_abort(); // Run in the background
set_time_limit(0); // Run continuously
$interval=30; // Set the timing value
do {
//Do what you want to do
sleep($interval); // Wait for the waiter
}while(true);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/889137.htmlTechArticleHow to implement scheduled tasks in PHP, and how to implement tasks in PHP. This article describes how to implement scheduled tasks in PHP, and the code Simple and practical. Share it with everyone for your reference. Specific implementation...
Related labels:
source:php.cn
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
Popular Tutorials
More>
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!