Use scheduled tasks in Windows to automatically execute PHP program instance _ PHP Tutorial

Source: Internet
Author: User
Tags mysql backup
Use scheduled tasks in Windows to automatically execute PHP program instances. The so-called task plan is to use a computer to automatically call the user's preset applications, so as to simplify user operations. The use of Windows2000 Task Scheduler (equivalent to the so-called Task Scheduler, the computer automatically calls the user's preset application, so as to simplify user operations. We can schedule any script, program, or document to run at the most appropriate time using the Windows 2000 Task Scheduler (equivalent to the cron program under * NIX, which is not detailed here, to meet your own needs. The following uses Windows 2000 as an example.

Specifically, if you need to use the Task Scheduler to run automatically, perform the following steps:

Click Start, select "program"> "appendix"> "system tools"> "task Plan" (or "set"> "control panel"> "task Plan "), start the task scheduler management program for Windows 2000.
In the "Task Scheduler" window, double-click the "Add Task Scheduler" icon to start the "Task Scheduler wizard" of the system, and then click "next, select the application to be automatically run in the program list, and click "next.
Set the appropriate task plan name and select the time frequency for automatic execution of the task (such as daily, weekly, monthly, one-time, every start of the computer, and every login time ), click "next.
At this time, the system will require the user to set the specific time for the program to run, such as the number, the number of minutes, and the time period to run. we only need to set the time according to our own needs.
Next, the system requires the user to set the appropriate user name and password (as shown in figure 5) so that the system can run automatically in the future.
Finally, you only need to click "finish" to add the corresponding task to Windows.
In 2000 of the Task Scheduler, it will automatically "remember" this task, once the system time and related conditions match the plan set by the user, it automatically calls the application specified by the user.
(Every Time Windows 2000 is started, the task scheduler starts automatically and runs in the background to ensure that your plan can be executed on time ).
Now let's test whether the task we just created is successful. right-click the "php" program icon (6) and choose "run" from the pop-up menu ". Generally, this is the only option for the program icon.
You can start the instance properly after activation. If the operation fails, check whether the user and password are set correctly.
Scheduler "indicates whether the Scheduler service has been started. I originally switched it off to save system resources, leading to Operation failure. I have been searching for it for a long time. In addition, you can view the original information in system logs.
The operation fails.

Now we have discussed the application of such a multi-task plan. here are two examples:

I. make PHP run regularly

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

The code is as follows:

$ Fp = @ fopen ("test.txt", "a + ");
Fwrite ($ fp, date ("Y-m-d H: I: s"). "Let PHP run regularly! \ N ");
Fclose ($ fp );
?>

Add a Task Scheduler and enter the following command in step (2:

The code is as follows:

D: \ php4 \ php.exe-q D: \ php4 \ test. php

Set the time to run the task once every one minute.
Now let's see if the content in the d: \ php4 \ test.txt file is successful. If the content is as follows, congratulations.

The code is as follows:


11:08:01 let PHP run on a regular basis!
11:09:02 let PHP run on a regular basis!
11:10:01 let PHP run on a regular basis!
11:11:02 let PHP run on a regular basis!

II. enable automatic MYSQL backup

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

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:

Can be database name you wowould like to backup.
With the -- help, or -? Options, you can get this help and exit.
} Else {
$ Dbname = $ argv [1];
$ Dump_tool = "c: \ mysql \ bin \ mysqldump ";
$ Rar_tool = "d :\\ php4 \ rar ";
@ Exec ("$ dump_tool -- opt-u user-ppassword $ dbname>./$ dbname. SQL ");
@ Exec ("$ rar_tool a-ag_yyyy_mm_dd_hh_mm mongodbname.rar $ dbname. SQL ");
@ Unlink ("$ dbname. SQL ");
Echo "Backup complete! ";
}
?>


Add a Task Scheduler and enter the following command in step (2:

The code is as follows:

D: \ php4 \ php.exe-q D: \ php4 \ backup. php databasename

Set the time to run once a day and then run the task.
Finally, an rarfile consisting of the database name and current time will be generated in the d: \ php4 \ directory.
Congratulations! Success!
Of course, there are many backup methods, so readers can do what they like!

The above is the original work. in combination with my own practices, the supplementary instructions are as follows:

If an error occurs:


An error occurred while trying to set the task account information.
The specified error is:
0x80070005: Access Denied
You do not have permission to run the requested operation

In the above '"4. Next, the system will ask the user to set the appropriate user name and password so that the system can run automatically in the future". it is best to use the "system" user here, the password can be blank.
This system has a very high level of permissions, which is higher than your administrator, so you should never mess up when running the command, but if there is no prompt, it will be executed unconditionally, you can kill the core process with this permission.

2. add a task scheduler and enter the following command in this step:

The code is as follows:


D: \ php4 \ php.exe-q D: \ php4 \ test. php

The correct format should be

The code is as follows:


"D: \ php4 \ php.exe"-q "D: \ php4 \ test. php"

That is, the path should be enclosed in double quotation marks.

Recently, I have made several PHP game projects, including card and board games and rpg games, all of which require more or less timed information update mechanisms. For example, player timeout detection for card and board games. More are used in rpg games, such as Monster refresh, automatic blood returning, task expiration, and ranking refresh. Because PHP does not have a memory resident program, it has some difficulties in processing it.

I have referred to some implementation methods in the same industry. The usual practice is to write an auxiliary program in c ++, python, java, etc. based on the needs of a specific project to regularly update the database. However
Yes. First, these helper programs require the intervention of programmers who understand another language, which will inevitably increase development costs and risks. Second, it is difficult for programmers in different languages to conduct joint debugging.
The relationship between the auxiliary program and the foreground is very close, basically need to be developed at the same time, debugging together.

I used a regular task execution method in the project. I feel that this solution is good and belongs to the permanent type. I handed all the code to the PHP side.

First, define a table named task in the database, which contains two fields: exectime and
Url. Exectime is a unix-type time, and url is string-type. Each piece of data represents a task. Specifically, "This task is executed at exectime,
The executed url is ". The helper monitors the table every second and compares the current time to the time of each task in the table. if the time reaches, the helper requests the url, and the task is completed and deleted.
This task. This is cyclical.

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

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

Additional content:

The task is enabled in this way. we have a server switch interface similar to a large online game. after logging on to the game background, go to the server control page to view the running status of the current server, you can enable or disable the server. When the server is enabled, related tasks are inserted into the task list. if the server is disabled, the task list is cleared. It is in the artificial form.


The task is enabled repeatedly, because these tasks are inserted into the task table by php, and each task in the task table is deleted by the auxiliary program once it is executed, therefore, each task can only be executed once. If a task needs to be executed cyclically, you can only insert the task itself into the task list by executing the php code of the task (that is, the task url.


Task timeout: There are two types of task timeout: in the data table, task execution times out, and in the request for this task page times out. The first case does not occur because the helper executes all tasks less than or equal to the current time each time. In the second case, the helper program automatically determines whether the access to the page is successful. if a server error is returned or cannot be connected, the task will be retained and not deleted, wait until the next loop.

Bytes. Use the job scheduler of Windows 2000 (equivalent...

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.