The following is the code for the implementation of scheduled tasks in pure PHP, which is popular on the internet. I have improved it, but don't rush to use it first. You must see the bottom.
| The code is as follows: |
Copy code |
|
<? Php
Ignore_user_abort (); // close the browser and run the PHP script.
Set_time_limit (600); // set the execution time of the program to 600 seconds.
// Set_time_limit (0); // sets the execution time of the program to an infinite length.
$ Interval = 5; // run every 5 seconds
Do {
$ Fp = fopen('sjyhome.txt ', 'A'); // open sjyhome.txt
Fwrite ($ fp, 'Welcome to SJY home www.111cn.net '); // write information to sjyhome.txt
Fclose ($ fp); // close sjyhome.txt
Sleep ($ interval); // wait for 5 seconds
} While (true );
?> |
The above code is an endless loop, and even deleting the code file does not help. You must restart the WEB to stop script execution. Therefore, we need a script execution switch, which can be implemented using the method introduced by external files. When the while loop is running, the include switch variable is enough.
Create an external variable file switch. php with the following content:
| The code is as follows: |
Copy code |
<? Php
Return 1; // 1 run the script 0 exit the execution script
?>
Improved PHP code
<? Php
Ignore_user_abort (); // close the browser and run the PHP script.
Set_time_limit (600); // set the execution time of the program to 600 seconds.
// Set_time_limit (0); // sets the execution time of the program to an infinite length.
$ Interval = 5; // run every 5 seconds
$ Switch = include 'switch. Php ';
While ($ switch ){
$ Fp = fopen('sjyhome.txt ', 'A'); // open sjyhome.txt
Fwrite ($ fp, 'Welcome to SJY home www.111cn.net $ switch \ n'); // write information to sjyhome.txt
Fclose ($ fp); // close sjyhome.txt
Sleep ($ interval); // wait for 5 seconds
$ Switch = include 'switch. Php ';
}
Exit ();
?>
|
Changing the return value in the switch. Php file to 0 will automatically stop running. To execute the command again, you must change the value to 1 and open the browser to execute the command again.
If the value of $ switch is 1, even if ignore_user_abort () is not set, the loop cannot be stopped even if the browser is closed, and it is useless to delete the code file. It can only wait for timeout (30 seconds by default, set_time_limit custom execution time) restart the WEB service.
Words written below
This method can implement the timing function, but once the code file is run, the process has been running, and I have not tested the consumption of server resources, however, some space providers may not allow long-running processes.
If you have friends with VPS or servers, we recommend that you plan tasks on the server to execute scripts on your website.
The following describes how to use Crontab in Linux.
For more information about how to use crontab, see: Linux scheduled task crontab.
1. Use PHP to execute scripts in Crontab
Just like calling a common shell script in Crontab (specific Crontab usage), you can use a PHP program to call the PHP script.
Execute myscript. php every hour as follows:
| The code is as follows: |
Copy code |
# Crontab-e
00 ***/usr/local/bin/php/home/web/3aj. php
/Usr/local/bin/php is the path of the PHP program.
|
2. Use the URL in Crontab to execute the script
If your PHP script can be triggered through a URL, you can use lynx, curl, or wget to configure your Crontab.
The following example uses the Lynx text browser to access the URL and execute the PHP script hourly. By default, the Lynx text browser opens a URL in dialog mode. However, as shown below, we use the-dump option in the lynx command line to convert URL output to standard output.
00 ***** lynx-dump http: // your URL/script. php
The following example uses CURL to access the URL and execute the PHP script every 5 minutes. Curl displays the output in standard output by default. With the "curl-o" option, you can also dump the script output to a temporary file.
*/5 */usr/bin/curl-o temp.txt http: // your URL/script. php
The following example uses the wget url to execute the PHP script every 10 minutes. -The q option indicates quiet mode ." -O temp.txt "indicates that the output will be sent to a temporary file.
*/10 */usr/bin/wget-q-O temp.txt http: // your URL/script. php
Next, we will introduce how to implement php tasks in Windows.
1. Create a new 3aj. php with the following content:
| The code is as follows: |
Copy code |
<?
$ Fp = fopen ("3aj.txt", "a + ");
Fwrite ($ fp, date ("Y-m-d H: I: s"). "success! \ N ");
Fclose ($ fp );
?>
|
2. Create a 3aj. bat file with the following content:
C: \ php \ php.exe-q D: \ web \ 3aj. php
3. Create a WINDOWS scheduled task:
Start> Control Panel> Task Scheduler> Add Task Scheduler
Browse the folder and select the above 3aj. bat file
Set the time and password (log on to WINDOWS and save it.
4. Right-click the scheduled task and choose "run".