Several methods for PHP to implement scheduled tasks

Source: Internet
Author: User
There are two methods for executing a scheduled task php program: ignore_user_abort for scheduled operations and Crontab for linux, use the task plan for windows machines... there are two methods for executing a scheduled task php program: ignore_user_abort for scheduled operations and Crontab for linux, use the task plan for windows machines.

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. you must see the following code:

 

The above code is an endless loop. even if you delete the code file, it will not help. you must restart the WEB to stop the script execution. Therefore, we need a script execution switch, we can use the method introduced by an external file to implement this function. during the while loop, you can include the switch variable.

Create an external variable file switch. php with the following content:

  

Improved PHP code:

   

Changing the return value in the switch. php file to 0 will automatically stop running. to execute it again, you must change the value to 1 and open the browser to execute it 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. the default value is 30 seconds, set_time_limit: Customize the execution time and restart the WEB service.

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 vendors may not allow long periods of busy processes. if you have friends with VPS or servers, we recommend that you plan tasks on the servers 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 common shell scripts in Crontab (specific Crontab usage), PHP programs are used to call PHP scripts.

Execute myscript. php every hour as follows:

# 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. 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.

00 ***** lynx-dump http: // your URL/script. php

The following example uses the CURL access URL to execute the PHP script every 5 minutes. The Curl displays the output in the standard output by default. The "curl-o" option is used, 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, and "-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 the implementation of php tasks in Windows:

1. create a new 3aj. php with the following content:

    

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

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


Article address:

Reprint at will ^ please include the address of this article!

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.