PHP does not support multithreading. sometimes it is not so nice to handle the problem. today, let's talk about three methods of PHP scheduled execution: PHP scheduled execution.
1. windows scheduled tasks
2. linux script program
3. regularly refresh web browsers
Implementation
Windows scheduled task
PHP rarely run on the Windows Server, the specific implementation is not further study, see the principle of online implementation is probably write bat script, and then let the window task to add the execution of the bat script, the specific can refer to: http://www.jb51.net/article/29134.htm
Linux script implementation
The crontab command is used here,
Usage:
Crontab filecrontab [-u user] [-u user] {-l |-r |-e}
Note:
Crontab is used to allow users to run programs at a fixed time or interval.
Use crontab to write the shell script and then let PHP call the shell. this is based on the linux feature and should not be a feature of the PHP language.
See: http://www.jb51.net/article/29136.htm
Scheduled execution of tasks in PHP
Several problems need to be solved to refresh the browser using php
PHP script execution time limit, the default solution is 30 m: set_time_limit (); or modify PHP. ini to set max_execution_time (not recommended)
If the browser of the client is closed, the program may be terminated. Solution: ignore_user_abort can still run normally even if the page is closed.
If the program runs continuously, it may consume a lot of resources. the solution is to use sleep to sleep the program for a while, and then run
PHP code regularly executed:
The code is as follows:
Ignore_user_abort (); // close the browser and run the PHP script.
Set_time_limit (3000); // you can use set_time_limit (0) to run the program without restrictions.
$ Interval = 5; // run every 5s
// Method 1 -- Endless loop
Do {
Echo 'test'. time ().'
';
Sleep ($ interval); // wait for 5 s
} While (true );
// Method 2 --- regular sleep execution
Require_once './curlClass. php'; // introduce the file
$ Curl = new httpCurl (); // instantiate
$ Stime = $ curl-> getmicrotime ();
For ($ I = 0; $ I <= 10; $ I ++ ){
Echo 'test'. time ().'
';
Sleep ($ interval); // wait for 5 s
}
Ob_flush ();
Flush ();
$ Etime = $ curl-> getmicrotime ();
Echo '';
Echo round ($ etime-stime), 4); // program execution time
During the test, we found that the efficiency was not very high,
Summary:
I personally feel that the efficiency of PHP scheduled task execution is not very high. I suggest you give it to shell for the scheduled task execution.
Ps: the method of the endless loop seems to be a commonly used method for malicious website attacks.