This section 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/abc. 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.
| The code is as follows: |
Copy code |
00 *** lynx-dump http://abc.cn/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.
| The code is as follows: |
Copy code |
*/5 */usr/bin/curl-o temp.txt http://abc.cn/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.
| The code is as follows: |
Copy code |
*/10 ***/usr/bin/wget-q-O temp.txt http://abc.cn/script.php |
Next, we will introduce how to implement php tasks in Windows.
1. Create an abc. php file with the following content:
| The code is as follows: |
Copy code |
<? $ Fp = fopen ("abc.txt", "a + "); Fwrite ($ fp, date ("Y-m-d H: I: s"). "success! /N "); Fclose ($ fp ); ?> |
2. Create an abc. bat file with the following content:
| The code is as follows: |
Copy code |
C:/php/php.exe-q D:/web/abc. php |
3. Create a WINDOWS scheduled task:
Start> Control Panel> Task Scheduler> Add Task Scheduler
Browse the folder and select the above abc. bat file
Set the time and password (log on to WINDOWS and save it.
4. Right-click the scheduled task and choose "run".
Next, we will introduce a scheduler task implemented using the php program.
This efficiency is not high, and it is unstable. It is King to use crontab.
| The code is as follows: |
Copy code |
<? Php Ignore_user_abort (); // close the browser and run the PHP script. Set_time_limit (3000); // set the execution time of the program to 3000 seconds. // Set_time_limit (0); // sets the execution time of the program to an infinite length. $ Interval = 30; // run every 30 seconds Do { $ Fp = fopen('abc.txt ', 'A'); // open abc.txt Fwrite ($ fp, 'Hello, I'm from http://www.111cn.net '); // write information to abc.txt Fclose ($ fp); // close abc.txt Sleep ($ interval); // wait 30 seconds } While (true ); ?> |
First run the page, then close the page, and the program is still running. Every 30 seconds, the program will fill in the information to go to the abc.txt file.