Php scheduled task example

Source: Internet
Author: User
1. execute the script using PHP in Crontab, just like calling a common shell script in Crontab. for specific Crontab usage, use the PHP program to call the PHP script and execute myscript every hour. ph... 1. use PHP to execute scripts in Crontab

Just like calling a common shell script in Crontab, the specific Crontab usage uses a PHP program to call the PHP script. execute myscript. php every hour as follows:

#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. the Lynx text browser opens the URL in dialog mode by default. 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:

00 *** lynx-dump http://abc.cn/script.php

The following example uses the CURL access URL to execute the PHP script every five minutes. The Curl displays the output in the standard output by default and uses the "curl-o" option, you can also dump the script output to a temporary file. the code is as follows:

*/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, and "-O temp.txt" indicates that the output will be sent to a temporary file. the code is as follows:

*/10 ***/usr/bin/wget-q-O temp.txt http://abc.cn/script.php

Next, we will introduce the implementation of php tasks in Windows.

1. create an abc. php file with the following code:

 

2. create an abc. bat file with the following content:

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, and log on to WINDOWS to save it.

4. right-click the scheduled task and choose "run ".

Next, we will introduce a scheduler task implemented using a php program. This efficiency is not high and it is unstable. it is King to use crontab. the code is as follows:

  

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.