Windows use Scheduled tasks to automate PHP program instance _php instance

Source: Internet
Author: User
Tags rar system log

The so-called task plan is that the computer automatically calls the user to set up the application in advance, so as to simplify the user's operation. With the Windows 2000 Task Scheduler (which is no longer detailed here with the Cron program under *nix), we can schedule any script, program, or document to run at the most appropriate time to meet our needs. Let's take Windows 2000 for example.

Specifically, if we need to run the Task Scheduler automatically, we should perform the following steps:

Click the Start button, and then select programs → attachments → system tools → task scheduler (or settings → control Panel → Task Scheduler) to start the Task Scheduler management program for Windows 2000.
In the Scheduled Task window, double-click the Add Task Schedule icon, start the Task Scheduler Wizard for the system, click Next, select the application that you want to run automatically in the list of programs that you give, and then click Next.
Set the appropriate task schedule name and select the frequency of time to automate this task, such as Daily, weekly, monthly, one-time, every time you start your computer, and so on, and then click the Next button.
At this point, the system will require the user to the program to run the specific time to set, such as the number, the clock, which time to run, and so on, we just need to set according to their own needs.
The system will then require the user to set the appropriate username and password (as shown in Figure 5) so that the system can be run automatically in the future.
Finally, we simply click the Finish button to add the task to Windows
2000 of the Task Scheduler, it will automatically "remember" this task, once the system time and related conditions and the user set up the plan, it will automatically invoke the user specified application, very square
(Each time you start Windows 2000, the Task Scheduler starts automatically and runs in the background to ensure that the user's plan is executed on time).
Now let's test whether the task we just built was successful, right-click the "PHP" program icon (as shown in Figure 6), and select "Run" in the pop-up menu. In general, the program icon as long as this
Activation can start normally. If the run fails, you can see if the user and password are set correctly, and determine the Task
Scheduler "Service has been started, I was in order to save the system resources to turn it off, cause the operation failed, I found a long day. In addition, you can see from the System log what the original
caused by the failure of the operation.

Okay, so much for the application of the mission plan, now that we cut to the chase, here are two examples:

First, let PHP run regularly

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

Copy Code code as follows:

<?php
$fp = @fopen ("Test.txt", "A +");
Fwrite ($fp, Date ("Y-m-d h:i:s"). "Let PHP run at regular intervals!" \ n ");
Fclose ($FP);
?>

Add a task schedule and enter the command in this step (shown in Figure 2):

Copy Code code as follows:

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

The time is set to run every 1 minutes, and then run this task.
Now let's see if the contents of the D:\php4\test.txt file are successful. If the content is as follows, congratulations on your success.

Copy Code code 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!

Second, let MySQL implement automatic backup

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

Copy Code code as follows:

<?php
if ($argc!= 2 | | in_array ($ARGV [1], Array ('--help ', '-? ')) {
?>
Backup Ver 0.01, for win95/win98/winnt/win2000/winxp on I32
Copyright (C)-Ptker All rights Reserved.
This are free Software,and your are welcome to modify and redistribute it
under the GPL license

PHP Shell script for the backup MySQL database.

Usage: <?php echo $argv [0];?> <option>

<option> can is database name you would like to backup.
with the--help, or-? options, you can get this help and exit.
<?php
} 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 $dbname. rar $dbname. sql");
@unlink ("$dbname. sql");
echo "Backup complete!";
}


Add a task schedule and enter the command in this step (shown in Figure 2):
Copy Code code as follows:

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

The time is set to run once a day and then run this task.
Finally, a RAR file consisting of database name and current time is generated in the D:\php4\ directory.
Congratulations to you! It's done!
Of course there are many ways to backup, readers can do according to their favorite to do!

The above is the original. Combined with my real cheap, the supplementary note is as follows:

If an error occurs:


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

On the top ' 4. The system will then require the user to set the appropriate username and password, so that the system can automatically run in the future. It is best to use the "system" user and the password may be blank.
This system has a very high privilege, higher than your administrator, so you should not do anything when you run the command, this is not an unconditional implementation of any hint, this permission under you kill the core process is OK.

Above 2, add a task plan, enter the command in this step:

Copy Code code as follows:

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

The correct form should be

Copy Code code as follows:

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

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

Recently done a few PHP game items, there are chess games also have RPG games, which are more or less need some time to update the information mechanism. For example, chess game player Timeout detection. RPG game used more, monster refresh, automatic gyrus, task expiration, list refresh and so on. Because PHP does not have a memory-resident program, there are some difficulties in handling it.

I refer to a number of peer implementation methods, the usual practice is based on the needs of specific projects, with C + +, Python, Java, etc. to write an auxiliary program to update the database regularly. But
It is very troublesome to do so. First of all, these helper programs need to understand the other language programmers involved, it is bound to increase the cost of a certain development and risk. Second, there is a lot of trouble and slow progress between programmers in different languages,
The relationship between the auxiliary program and the foreground is very close, which needs to be developed and debugged together.

I used a project in a time to carry out the task of the method, I feel that this scheme is better, belong to the permanent type, all the code to the PHP side.

First, in the database, define a table named task with two fields Exectime and
Url. Where Exectime is a UNIX type of time, the URL is a string type. Each piece of data represents a task, meaning "this task executes at Exectime,
The address to execute is URL ". The helper monitors the table every second, compares the current time to the time of each task in the table, and, if the time is reached, requests the URL, and then the task executes and deletes
This task. This cycle.

The advantage of this is that PHP developers are free to perform the pages they want to execute at the time they want. And this program only needs to write once, put in any similar project can be very good use.

I've made this program a daemon of Windows services and ArchLinux, which enables the entire project to be cross-platform.

Supplementary content:

The task of the Open is like this, we do a similar large-scale game of the server switch interface, login game backstage, to the server control page, you can view the current server running state, you can turn on or off the server. To turn on the server is to insert related tasks into the task list, and shutting down the server is to empty the task list. is artificial form.


Tasks are repeatedly turned on because these tasks are inserted into the task table by PHP, and each task in the task table is executed once by the helper, so each task can only be executed once. If a task needs to be cycled, it can only be done by inserting himself into the task list in the PHP code that performs the task (the URL of the task).


Task timeout, the task timeout is divided into two types, the data table, the task between the execution timeout, one is to request the task page timeout. The first scenario does not occur because the helper performs all tasks that are less than or equal to the current time each time. In the second case, the helper automatically determines whether the access to this page is successful, and if you return a server error or cannot connect, keep the task, do not delete it, and wait until the next loop to try to execute it.

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.