Examples of using scheduled tasks to automatically execute PHP programs in Windows_PHP Tutorial

WBOY
Release: 2016-07-13 10:30:07
Original
1040 people have browsed it

The so-called task plan is for the computer to automatically call the applications set by the user in advance, thereby simplifying the user's operations. Using the task scheduler of Windows 2000 (equivalent to the cron program under *NIX, which will not be described in detail here), we can schedule any script, program or document to run at the most appropriate time to meet our needs. The following takes Windows 2000 as an example.

Specifically, if we need to use the task scheduler to run automatically, we should perform the following steps:

Click the "Start" button, then select "Programs" → "Accessories" → "System Tools" → "Task Scheduler" (or "Settings" → "Control Panel" → "Task Scheduler") to start Windows 2000 task planning management program.
Double-click the "Add Task Plan" icon in the "Task Plan" window to start the system's "Task Plan Wizard", then click the "Next" button and select the application that needs to be run automatically in the given program list , and then click the "Next" button.
Set an appropriate task schedule name and select the time frequency to automatically perform this task (such as daily, weekly, monthly, one-time, every time you start the computer, every time you log in, etc.), then click "Next" "Button.
At this time, the system will ask the user to set the specific time for the program to run, such as what day, what time, what time period it can run, etc. We only need to set it according to our own needs.
Next, the system will ask the user to set an appropriate username and password (as shown in Figure 5) so that the system can run automatically in the future.
Finally, we only need to click the "Finish" button to add the corresponding task to the task scheduler of Windows
2000. After that, it will automatically "remember" the task once the system time and related conditions If it matches the plan set by the user, it will automatically call the application specified by the user, which is very convenient (every time you start Windows 2000, the task scheduler will automatically start and run in the background to ensure that the user's plan able to execute on time).
Now let’s test whether the task we just created is successful. Right-click the "php" program icon (as shown in Figure 6) and select "Run" in the pop-up menu. Under normal circumstances, the program icon can start normally as long as it is
activated and running. If the operation fails, you can check whether the user and password are set correctly, and confirm whether the "Task
Scheduler" service has been started. I turned it off in order to save system resources, which caused the operation to fail, which cost me a long time to find. In addition, you can also check the "System Log" to see what
caused the operation to fail.

Okay, after talking about so many applications of task planning, let’s get to the point. Two examples will be introduced below:

1. Let PHP run regularly

Edit the following code and save it as test.php:


Copy the code The code is as follows:
$fp = @fopen("test.txt", "a+");
fwrite($fp, date("Y-m-d H:i:s") . " Let PHP Run it regularly! n");
fclose($fp);
?>
Add a task plan and enter the command at this step (as shown in Figure 2):


Copy the code The code is as follows:
D:php4php.exe -q D:php4test.php

Set the time to run every 1 minute and then run this task.

Now let’s see if the content of the d:php4test.txt file was successful. If the content is as shown below, congratulations on your success.

Copy code The code is as follows:
2003-03-03 11:08:01 Let PHP run regularly!
2003-03-03 11:09:02 Let PHP run regularly!
2003-03-03 11:10:01 Let PHP run regularly!
2003-03-03 11:11:02 Let PHP run regularly!
2. Let MYSQL realize automatic backup

Edit the following code and save it as backup.php. If you want to compress it, you can copy a rar.exe:

Copy code The code is as follows:

if ($argc != 2 || in_array($argv[1], array('--help', '-?'))) {
?>
backup Ver 0.01, for Win95/Win98/WinNT/Win2000/WinXP on i32
Copyright (C) 2000 ptker All rights reserved.
This is free software,and you are welcome to modify and redistribute it
under the GPL license

PHP Shell script for the backup MySQL database.

Usage:


添加一个任务计划,在(如图2所示)这一步输入命令:
复制代码 代码如下:

D:php4php.exe -q D:php4backup.php databasename

时间设置为每天运行一次,然后运行这个任务。
最后会在d:php4目录下生成一个以数据库名和当前时间组成的rar文件。
恭喜你!大功告成了!
当然备份方式有很多种,读者可按照自己喜欢的去做!

以上是原著.结合本人实贱,补充说明如下:

如果出现错误:


在试着设置任务帐户信息时出现错误
指定的错误是:
0x80070005:拒绝访问
您没有运行所请求的操作的权限

在上面'"4.接下来系统将会要求用户设置适当的用户名及密码,以便系统今后能自动加以运行".这里最好用"system"用户,密码可为空.
这个system的权限非常之高,比你的administrator还要高,所以你在运行命令的时候千万不要乱来,这个可是什么提示都没有就会无条件执行的,这个权限下你kill核心进程都行.

上面2、添加一个任务计划,在这一步输入命令:

复制代码 代码如下:

D:php4php.exe -q D:php4test.php

正确形式应为

复制代码 代码如下:

"D:php4php.exe" -q "D:php4test.php"

That is, the path must be enclosed in double quotes.

I have recently made several PHP game projects, including chess and card games and RPG games, all of which more or less require mechanisms for regularly updating information. For example, player timeout detection in chess and card games. It is used more in RPG games, such as monster refresh, automatic blood recovery, task expiration, ranking refresh, etc. Because PHP does not have memory-resident programs, there are some difficulties in processing.

I referred to the implementation methods of some peers. The usual approach is to write an auxiliary program in c++, python, java, etc. to update the database regularly according to the needs of the specific project. But
it is very troublesome to do so. First of all, these auxiliary programs require the intervention of programmers who know another language, which will inevitably increase development costs and risks. Second, joint debugging between programmers of different languages ​​is very troublesome and the progress is very slow. Since the relationship between the auxiliary program and the front desk is very close, they basically need to be developed and debugged together.

I adopted a method of regularly executing tasks in the project. I feel that this solution is better, it is a once-and-for-all type, and all the code is handed over to PHP.

First, define a table named task in the database, which has two fields exectime and

url. Among them, exectime is a unix type time, and url is a string type. Each piece of data represents a task, and the specific meaning is "this task is executed during exectime, and the execution address of
is url". The auxiliary program will monitor this table every second, compare the current time with the time of each task in the table, if the time is reached, request the url, then the task execution is completed, and the
task will be deleted. And so on.

The advantage of this is that PHP program developers can freely execute the web pages they want to execute at the time they want. And this program only needs to be written once and can be used well in any similar project.

I made this program into a windows service and archlinux Daemon, thus realizing the cross-platform of the entire project.

Supplementary content:

The task is started like this. We made a server switch interface similar to that of large-scale online games. After logging in to the game backend, go to the server control page and you can check the running status of the current server and turn the server on or off. Starting the server inserts related tasks into the task list, and closing the server clears the task list. It is artificial form.

Repeated opening of tasks, because these tasks are inserted into the task table by php, and each task in the task table is executed once and then deleted by the auxiliary program, so each task only Can be executed once. If there is a task that needs to be executed in a loop, then the only way is to re-insert itself into the task list in the PHP code that executes the task (that is, the URL of the task).

Task timeout. Task timeout is divided into two types. In the data table, there is a timeout between task executions. One is the timeout when requesting the task page. The first case does not happen because the helper program executes all tasks up to and including the current time each time. In the second case, the auxiliary program will automatically determine whether the access to this page is successful. If a server error is returned or the connection cannot be made, the task will be retained without deletion and will be tried again in the next cycle.

http://www.bkjia.com/PHPjc/767613.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/767613.htmlTechArticleThe so-called task plan is to automatically call the application program set by the user in advance by the computer, thereby simplifying the user's operation. Utilize Windows 2000's Task Scheduler (comparable to...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template