Php implementation of scheduled task execution

Source: Internet
Author: User

This article summarizes some common implementation methods for scheduled task execution in php, including using php directly, and using scheduled tasks in the operating system, for more information, see.

Every PHP script limits the execution time, so we need to use set_time_limit to set the execution time of a script to an infinite length, and then use flush () and ob_flush () to clear the Server Buffer, output the return value of the script at any time. After execution, we get a line of Hello World every five seconds.

The Code is as follows: Copy code

Header ("Content-Type: text/plain ");
Set_time_limit (0 );

$ InfoString = "Hello World". "n ";
While (isset ($ infoString ))
{
Echo $ infoString;
Flush ();
Ob_flush ();
Sleep (5 );
}

Example

The Code is as follows: Copy code

<? Php
Ignore_user_abort (); // The PHP script can be executed even if the Client is disconnected (such as the browser is disabled.
Set_time_limit (0); // the execution time is unlimited. The default php Execution time is 30 seconds. You can use set_time_limit (0) to limit the execution of the program.
$ Interval = 20; // time interval in seconds
$ Key_file = "key.txt"; // configuration file
If (isset ($ _ GET ['s '])
{
If ($ _ GET ['s '] = "0") {// stop the job, but do not exit
$ S = "false ";
Echo "Function is off ";
}
Elseif ($ _ GET ['s '] = "1") {// work
$ S = "true ";
Echo "Function is on ";
}
Elseif ($ _ GET ['s '] = "2") {// exit
$ S = "die ";
Echo "Function exited ";
}
Else
Die ("Err 0: stop working 1: working 2: exit ");
$ String = "<? Phpn return "". $ s. ""; n?> ";
Write_inc ($ key_file, $ string, true );
Exit ();
}
If (file_exists ($ key_file )){
Do {
$ Mkey = include $ key_file;
If ($ mkey = "true") {// if the job
///////////////// /////////////////
$ Showtime = date ("Y-m-d H: I: s ");
$ Fp = fopen('func.txt ', 'A ');
Fwrite ($ fp, $ showtime. "n ");
Fclose ($ fp );
//////////////////////////////////////// ///////////////////////////
}
Elseif ($ mkey = "die") {// If you exit
Die ("I am dying! ");
}
Sleep ($ interval); // wait $ interval minutes
} While (true );
}
Else
Die ($ key_file. "doesn't exist! ");
Function write_inc ($ path, $ strings, $ type = false)
{
$ Path = dirname (_ FILE _). "/". $ path;
If ($ type = false)
File_put_contents ($ path, $ strings, FILE_APPEND );
Else
File_put_contents ($ path, $ strings );
}
?>

I personally think it is still a bit complicated and difficult to execute tasks in php timing. Next I will introduce how to schedule tasks in the operating system.

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: Copy code

<?

$ 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: Copy code

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


In linux


We can use the Linux Crontab tool to trigger PHP Execution tasks stably and reliably.
The following describes two methods of 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:

1.

The Code is as follows: Copy code
# Crontab-e
2.00 ***/usr/local/bin/php/home/john/myscript. php
/Usr/local/bin/php

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
1.00 * lynx-dump/myscript. 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
1. */5 *****/usr/bin/curl-o temp.txt/myscript. php

The following example uses the wget url to execute the PHP script every 10 minutes. -Q indicates quiet mode. "-O temp.txt" indicates that the output will be sent to a temporary file.

The Code is as follows: Copy code

1. */10 *****/usr/bin/wget-q-O temp.txt/myscript. php


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.