PHP scheduled execution task-PHP source code

Source: Internet
Author: User
Tags php language
PHP scheduled execution of scheduled tasks PHP scheduled execution of scheduled tasks

PHP does not support multithreading. sometimes it is not so nice to handle the problem. let's talk about the regular PHP execution method today.

PHP scheduled execution in three ways
1. windows scheduled tasks
2. linux script program
3. regularly refresh web browsers

Implementation
Windows scheduled task

PHP rarely run on the Windows Server, the specific implementation is not further study, see the principle of online implementation is probably write bat script, and then let the window task to add the execution of the bat script, the specific can refer to: http://www.cnblogs.com/whoknows/articles/2228577.html

Linux script implementation

The crontab command is used here,

Usage:

crontab   filecrontab [ -u user ] [ -u user ] { -l | -r | -e }

Note:

Crontab is used to allow users to run programs at a fixed time or interval.

Use crontab to write the shell script and then let PHP call the shell. this is based on the linux feature and should not be a feature of the PHP language.

See: http://www.centos.bz/2011/07/php-cron-job-linux-crontab/

Scheduled execution of tasks in PHP

Php

Several problems need to be solved to refresh the browser using php

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)
If the browser of the client is closed, the program may be terminated. Solution: ignore_user_abort can still run normally even if the page is closed.
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 run

PHP code regularly executed:

 '; Sleep ($ interval); // wait for 5 s} while (true); // method 2 --- sleep regularly executes 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

The above is the content of the PHP scheduled execution task. For more information, see The PHP Chinese website (www.php1.cn )!

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.