Php scheduled task example

Source: Internet
Author: User
Tags curl php script

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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.