Implementation principle of scheduled scheduling task in php

  • 2020-05-27 04:29:47
  • OfStack

According to the php manual, I will briefly introduce 1 relevant knowledge:

1. Connection processing:

Inside PHP, the system maintains the connection state, and there are three possible states:

0-NORMAL (normal)
1-ABORTED (exception exit)
2 - TIMEOUT (timeout)

The connection is valid when the PHP script runs the NORMAL state normally. When the remote client breaks the connection, the ABORTED status flag is opened. The interruption of a remote client connection is usually caused by the user clicking the STOP button. When the connection time exceeds the PHP time limit, the flag for the TIMEOUT state is opened.

You can decide whether the script needs to exit when the client breaks the connection. Sometimes it is convenient to have the script run in its entirety, even if no remote browser accepts the script's output. By default, the script exits when the remote client connection is broken. This process can be controlled by the ignore_user_abort of php.ini or by the "php_value ignore_user_abort" and the ignore_user_abort() function corresponding to the Apache.conf Settings. If PHP is not told to ignore the user's interrupt, the script will be interrupted unless the shutdown trigger function is set via register_shutdown_function(). With this close trigger function, when the remote user clicks the STOP button and the script tries to output data again, PHP will detect that the connection has been broken and call the close trigger function.

Scripts can also be interrupted by built-in script timers. The default timeout limit is 30 seconds. This value can be changed by setting the max_execution_time or Apache.conf parameters corresponding to "php_value max_execution_time" or the set_time_limit() function in the php.ini max.ini max_execution_time or Apache.conf Settings. When the counter is timed out, the script exits similar to the connection interruption above, and the previously registered shutdown trigger function is executed. In this close trigger function, you can check whether the timeout caused the close trigger function to be called by calling the connection_status() function. If the timeout results in a call to close the trigger function, the function returns 2.

One thing to note is that both the ABORTED and TIMEOUT states are valid at the same time. This is possible when telling PHP to ignore the user's exit operation. PHP will still notice when the user has broken the connection but the script is still running. If the run time limit is reached, the script will exit and the set shutdown trigger function will be executed. At this point you will find that the function connection_status() returns 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 will return the previous setting and can be called without an argument to not change the current setting and only return the current setting.

int connection_aborted ( void )
Returns TRUE if client disconnected.

int connection_status ( void )
Returns the connection status bitfield.

In order to update a file regularly, the program needs to run automatically. Two methods were found on the web: ignore_user_abort() and crontab

The ignore_user_abort() function is combined with set_time_limit(0) and sleep($interval) to automatically run the update. Here is an example
 
ignore_user_abort(); // Even if the Client Disconnect the ( Close the browser ) . PHP The script can also continue to execute . 
set_time_limit(0); //  The execution time is unlimited, php The default execution time is 30 Second, through the set_time_limit(0) You can let the program run indefinitely  
$interval=60*5; //  every 5 Minutes to run  
do{ 
$fp = fopen('test.txt','a'); 
fwrite($fp,'test'); 
fclose($fp); 
sleep($interval); //  Waiting for the 5 minutes  
}while(true); 

Just run the above page, close it, and the program will run.

A simpler approach under Linux is the crontab command

The function of the crontab command is to schedule the execution of several commands at regular intervals.
crontab [-e |-l |-r] file name -e: edit task -l: display task information -r: delete scheduled task information

crontab format:
* * * * * Command
Commands to run at a time of day, month, and week

Examples of crontab:

*/5 * * * * lynx https://www.ofstack.com
Once every five minutes

0 8 * * * lynx https://www.ofstack.com
Visit www.ofstack.com every morning at 8 a.m

0 10 6 * 1-5 lynx https://www.ofstack.com
Visit www.ofstack.com at 10 a.m. on the 6th of each month and from the 1st to the 5th of each week

0 5 7 8 * lynx https://www.ofstack.com
Visit www.ofstack.com at 5 a.m. on 7 August

The above special meanings:
"*" stands for all Numbers in the range of values," /" stands for each,"*/5" stands for every 5 units," -" stands for going from one number to another number,"," separating a few discrete Numbers.

Related articles: