Home  >  Article  >  Backend Development  >  How to implement self-running in php

How to implement self-running in php

藏色散人
藏色散人Original
2021-07-15 10:37:062758browse

How to implement self-running in php: first create a PHP sample file; then enter "ignore_user_abort();"; finally pass "do{$fp = fopen('test.php','a'). ..}while(true)..." method can realize automatic execution of tasks.

How to implement self-running in php

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

How to realize self-running of php?

PHP Automatic Code Execution Method

Regarding how to automatically execute PHP code, we usually need to automatically execute the code when doing scheduled tasks, which is often achieved with the help of the system, such as linux's crontab Or the scheduled schedule of Windows, etc. Now I will share an automatic execution implemented in pure code.

The following is a bunch of PHP execution code used. The efficiency is not very good, but it can keep the task automatically executed.

<?php
    ignore_user_abort();
    // 即使client 断开(如关闭浏览器),PHP 脚本也可以继续执行。
    set_time_limit(0);
    $interval = 60*5;
    do{
        $fp = fopen(&#39;test.php&#39;,&#39;a&#39;);
        fwrite($fp,&#39;rn&#39;.date(&#39;Y-m-d H:i:s&#39;,time()).&#39;rn&#39;);
        fclose($fp);
        sleep($interval);
    }while(true)
    echo &#39;OK&#39;;

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to implement self-running in php. 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