Implementation of php scheduled tasks, supporting running in windows/linux-PHP source code

Source: Internet
Author: User
This article introduces how to use php to run scheduled tasks. It also introduces how to use winodows to schedule tasks in winows, and how to use linux in linux. This article introduces how to use php to run scheduled tasks. It also introduces how to use winodows to schedule tasks in winows, and how to use linux in linux.

Script ec (2); script

Several problems need to be solved to refresh the browser using php
1. PHP script execution time limit. The default solution is 30 m: set_time_limit (); or modify PHP. ini to set max_execution_time (not recommended)
2. If the client browser is closed, the program may be forced to terminate. Solution: ignore_user_abort can still run normally even if the page is closed.
3. If the program runs continuously, it may consume a lot of resources. The solution is to use sleep to sleep the program for a while, and then execute

The Code is as follows:

Ignore_user_abort (); // close the browser and run the PHP script.

Set_time_limit (3000); // you can use set_time_limit (0) to run the program without restrictions.

$ Interval = 5; // run every 5s

// Method 1 -- endless loop

Do {

Echo 'test'. time ().'

Sleep ($ interval); // wait for 5 s

} While (true );

// Method 2 --- regular sleep execution

Require_once './curlClass. php'; // introduce the file

$ Curl = new httpCurl (); // instantiate

$ Stime = $ curl-> getmicrotime ();

For ($ I = 0; $ I <= 10; $ I ++ ){

Echo 'test'. time ().'

Sleep ($ interval); // wait for 5 s


Ob_flush ();

Flush ();

$ Etime = $ curl-> getmicrotime ();

Echo '';

Echo round ($ etime-stime), 4); // program execution time

Set the PHP file for scheduled task execution in WINDOWS

I found some methods on the Internet to execute PHP scheduled tasks in WINDOWS. One of them was completely written, but unfortunately I did not pass it. Finally, I had to combine the methods of various schools to make the operation successful.

1. Write a PHP program named test. php. The content is as follows:

The Code is as follows:

$ Fp = fopen ("test.txt", "a + ");

Fwrite ($ fp, date ("Y-m-d H: I: s"). "success! N ");

Fclose ($ fp );


The program writes it boldly. It's okay to use javasderequire.
2. Create a Bat file named test. bat. The content is as follows:

The Code is as follows:
1 D: phpphp.exe-q D: websitetest. php

// Change the Directory

3. Create a WINDOWS scheduled task:
Start> Control Panel> Task Scheduler> Add Task Scheduler
Browse the folder and select the above bat file
Set the time and password (for WINDOWS login)
Save it.
4. over! Right-click the scheduled task and click "run ".

Run the PHP script on Linux 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:

# Crontab-e
00 ***/usr/local/bin/php/home/john/myscript. 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:
00 *** lynx-dump

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:
*/5 */usr/bin/curl-o temp.txt

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:
*/10 ***/usr/bin/wget-q-O temp.txt

Php scheduled task Introduction: I was particularly interested in se before a.m. on, but I didn't know java, lucene and other search engine development tools, so I kept exploring the functions of php.

Finally, we found that php can also be captured, and the principle is very easy: directly obtain the page source file, and then obtain the required information through the reference truncation of the regular expression or string. However, the performance cannot be compared with the multi-thread crawling of search engines.

After implementing the previous step, I thought again that if the capture can be automatically obtained at a scheduled time, the manual execution of the executable page would be saved.

Later, I learned about the effect of "scheduled tasks" in some open-source php programs: You can regularly run a program, such as database backup, update cache, generate static pages, and generate website maps.

Recently, because the project needs to regularly update the remote database to a local location, I found it online.

Ignore_user_abort (); functions can be used with set_time_limit (0); and sleep ($ interval); to achieve the above automatic update.

First, a basic paradigm is given, with a personal test program:

The Code is as follows:

Ignore_user_abort (); // run script in background
Set_time_limit (0); // run script forever
$ Interval = 30; // do every 15 minutes...
Do {
$ Fp = fopen('text3.txt ', 'A ');
Fwrite ($ fp, 'test ');
Fclose ($ fp );
Sleep ($ interval); // wait 15 minutes
} While (true );

First run the program, then close the page, and then fill in the text3.txt file every 30 seconds when the program is still running.

I personally feel that the efficiency of PHP scheduled task execution is not very high. I suggest you give it to shell for scheduled task execution.

Related 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: 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.