Php: How to Implement scheduled tasks, php scheduled tasks, the best solution, and php automatic tasks

Source: Internet
Author: User
Regular tasks have always been a problem for many friends in php, but they are encountered in many places. For example, in a game development program, players are sent to the game every 10 minutes. In the sns community, the system checks every 20 seconds to see if someone sends me an automatic message collection program, collect the latest Weibo data every five minutes.

Regular tasks have always been a problem for many friends in php, but they are encountered in many places. For example, in a game development program, players are sent to the game every 10 minutes. In the sns community, the system checks every 20 seconds to see if someone sends me an automatic message collection program, collect the latest Weibo data every five minutes.

Regular tasks have always been a problem for many friends in php, but they are encountered in many places.
For example:
In game development programs, send troops to players every 10 minutes
In the sns community, check if someone sends me a message every 20 seconds.
The automatic collection program collects the latest content every 5 minutes.
Weibo Data Synchronization: user data is synchronized every 10 hours through the Weibo api
......

There are several methods:
1. Linux
You can use cron to execute a php script or a script in other languages at regular intervals. [You must have sufficient system permissions]
Execute the edit cron command

1

crontab-e

For example, run the test. php script in the/root/bin directory every five minutes.

*/5 * php/root/bin/test. php

2. windows
Execute scripts for scheduled tasks.

3. If you do not have the system permission, let's take a look.
Because php is a weak interpreted language, it is not as easy as java to perform scheduled tasks. In java, a process can be directly assigned to the system to execute scheduled tasks, however, in php, there is no concept of multithreading in simple settings.

When I first came into contact with a scheduled task, a php friend asked me how to implement the scheduled task. The answer I gave at that time was to write a file containing the task, every time a task is executed, the file is called to check whether a task exists. However, if no task is triggered, the answer is obviously not perfect.

Later, I obtained the best solution through other channels, and I personally tested it.
The general idea is to set that the browser program will also stay in the server memory for execution, and the corresponding time will never expire.
Program Viewing:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

ignore_user_abort(TRUE);// Set to close the browser and execute the program

set_time_limit(0); // Set the response time to 30 seconds by default.

$count= 0;

while(TRUE)

{

sleep(5); // Execute every 5 seconds

// The file write operation starts.

$fp= fopen("test".$count.".txt","w");

if($fp)

{

for($i=0;$i<5;$i++)

{

$flag=fwrite($fp,$i."Here is the file content www.uacol.com \ r \ n");

if(!$flag)

{

echo"Failed to Write File";

break;

}

}

}

fclose($fp);

// The file write operation is complete.

$count++;

// Set the conditions for terminating a scheduled task

if(file_exists('lock.txt'))

{

break;

}

}

Name this file tast. php In the root directory of the website.
For example: www.uacol.com/tast.php
Visit this webpage. This program will create another website root directory named test0.txt,test1.txt,test2.txt... Text File
You can close the browser after the program is closed, without affecting the execution of the program. If you want to stop the program, put a file named lock.txt in the website root directory.
Example: www.uacol.com/lock.txt
Of course, restarting the server or restarting the computer can also prevent the program from continuing to run.

Related content:
Schtasks scheduled job for windows: http://www.cnblogs.com/lostyue/archive/2011/10/24/2223166.html

Cron service is a distributed timing service provided by SAE for developers: http://sae.sina.com.cn /? M = devcenter & catId = 195.

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.