Let's take a look at how to set scheduled tasks in uchome: 1. store all scheduled tasks in database 2 and execute a scheduled task in sequence each time you perform operations or open the page. that is, when the time is reached, it will be executed in the user process. the uchome scheduled task code is in. /source/function_cron.php only analyzes the uchome code's rough results about the scheduled task. Ignore_user_abort, it seems that most web applications do the same. Besides, there are several other types for reference 1. to provide stable triggers, use crontab + wget or AB to provide regular access 2, nohup + php write daemon 3, directly using php to write an endless loop and trigger it. in this case, you need to use the cache or database to assist in launching the loop, also, you must use sleep or usleep to control the trigger frequency 4, crontab + php5, and web trigger modes. this is also different. one is timed trigger, and the other is periodic trigger, DZ and PHPWind are both scheduled triggers. I used a periodic trigger. If you have a host, you must use crontab to execute the command line. PHP does not have to be written in the code. it would be better to use crontab. if you use a loop, you 'd better separate the loop from the specific handler, otherwise, the memory will continue to increase. This function-ignore_user_abort was found in the php Manual. This function can help us implement scheduled tasks like cron in linux. let's take a look at how to implement it. First, read the explanation of this function in the php Manual. Descriptionint ignore_user_abort ([bool $ setting]) Sets whether a client disconnect shoshould cause a script to be aborted. that is to say, the following program will be executed no matter whether the client closes the browser. Let's look at its parameters: ParameterssettingIf not set, the function will only return the current setting. this function accepts a parameter to determine whether to enable the ignore_user_abort function. Return value: Return ValuesReturns the previous setting, as a boolean. here, the previous setting is returned and bool is worth it. after my test, this statement is incorrect. the returned result is of the int type. How can we use the php function to implement scheduled tasks? This function is set_time_limit. you can use set_time_limit0 to set the program running time to no limit. the default php running time is 30 seconds, and set_time_limit (0) the program can be executed without limit. Before executing the program, add ignore_user_abort (1) and set_time_limit (0). how can I write the final program? Example:
- Ignore_user_abort (); // run script in background
- Set_time_limit (0); // run script forever
- $ Interval = 30; // do every 15 minutes...
- Do {
- $ Fp = fopen('text3.txt ', 'A ');
- Fwrite ($ fp, 'test ');
- Fclose ($ fp );
- Sleep ($ interval); // wait 15 minutes
- } While (true );
- ?>
|