Ignore_user_abort (), function with Set_time_limit (0), and sleep ($interval) to implement PHP scheduled tasks
First give a basic paradigm in which there are individual test procedures:
<?php
Ignore_user_abort (); Run script in background
Set_time_limit (0); Run Script Forever
$interval = 30; Do every minutes ...
do{
$fp = fopen (' Text3.txt ', ' a ');
Fwrite ($fp, ' test ');
Fclose ($FP);
Sleep ($interval); Wait minutes
}while (TRUE);
?>
First run the program, then close the page, the program is still running, and test will fill the Text3.txt file every 30 seconds.
Implementation effect (slightly)
Finally, according to the PHP manual a brief introduction of some relevant knowledge:
1. Connection handling:
Inside PHP, the system maintains a connection state with three possible conditions:
0-normal (Normal)
1-aborted (Abnormal exit)
2-timeout (Timeout)
The connection is valid when the PHP script is running normally in normal state. When the remote client disconnects, the token for the aborted status is opened. The interruption of a remote client connection is usually caused by the user tapping the STOP button. When the connection time exceeds the time limit of PHP, the TIMEOUT status token will be opened.
You can decide whether the script needs to exit when the client disconnects. Sometimes it is convenient to have the script run completely, even if no remote browser accepts the output of the script. The default scenario is that the script exits when the remote client connection is interrupted. This process can be controlled by the ignore_user_abort of the php.ini or by the corresponding "Php_value ignore_user_abort" and the Ignore_user_abort () function in the Apache conf settings. If you do not tell PHP to ignore the user's interruption, the script will be interrupted unless the shutdown trigger function is set by Register_shutdown_function (). With this shutdown trigger function, when the remote user taps the STOP button and the script attempts to output the data again, PHP will detect that the connection has been interrupted and invoke the shutdown trigger function.
Scripts can also be interrupted by a built-in script timer. The default time-out limit is 30 seconds. This value can be changed by setting the "Php_value max_execution_time" parameter or the Set_time_limit () function in the php.ini max_execution_time or Apache conf settings. When the counter times out, the script will exit similar to the above connection interruption, and the previously registered shutdown trigger function will be executed at this time. In the shutdown trigger function, you can check whether the timeout causes the shutdown trigger function to be called by calling the Connection_status () function. If the timeout causes a call to close the trigger function, the function returns 2.
One thing to note is that the aborted and TIMEOUT states can be valid at the same time. This is possible when telling PHP to ignore the user's exit operation. PHP will still be aware of the situation where the user has interrupted the connection but the script is still running. If the run time limit is reached, the script will be exited and the set off trigger function will be executed. At this point, the function connection_status () is found to return 3.
2. Related functions:
int Ignore_user_abort ([bool setting])
This function sets whether a client disconnect should cause a script to be aborted. It'll return the previous setting and can be called without a argument to don't change the current setting and only retur n the current setting.
int connection_aborted (void)
Returns TRUE if client disconnected.
int connection_status (void)
Returns the connection status Bitfield.
Introduction to PHP Timing schedule tasks