_php Tutorials for automating PHP program instances using Scheduled tasks in Windows

Source: Internet
Author: User
The so-called task plan is the computer automatically call the user pre-set application, so as to simplify the purpose of user operations. With the Task Scheduler for Windows 2000 (rather than the Cron program under *nix, which is no longer detailed here), we can schedule any script, program, or document to run at the most appropriate time to meet our needs. The following is an example of Windows 2000.

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

Start the Task Scheduler for Windows 2000 by clicking the Start button, and then selecting programs → accessories → system tools → task scheduler (or settings → control Panel → Task Scheduler) to launch the Tasks program manager.
In the Scheduled Tasks window, double-click the Add Scheduled task icon, launch the system's Task Scheduler Wizard, and then click the Next button, select the application that you want to run automatically in the list of programs that you have given, and then click Next.
Set the appropriate schedule name and select how often the task will be automatically performed (such as Daily, weekly, monthly, one-time, every start of the computer, every logon, and so on), and then click the Next button.
At this time the system will require users to run the program to set the specific time, such as the number of minutes, which several time period to run, we just need to set according to their own needs.
The system will then require the user to set the appropriate user name and password (5), so that the system can be automatically run in the future.
Finally, we simply click the "Finish" button to add the task to the Windows
2000 of the Task Scheduler, it will automatically "remember" this task, once the system time and related conditions and the user set the plan to match, it will automatically invoke the user's specified application,
(Each time you start Windows 2000, the Task Scheduler starts automatically and runs in the background to ensure that the user's schedule executes on time).
Now let's test the success of the task you just built, right-click on the "PHP" program icon (6) and select "Run" in the popup menu. In general, the program icon as long as this
The activation operation will start normally. If the run fails to see if the user and password are set correctly, there is also a OK Task
Scheduler "Service has been started, I was in order to save the system resources to turn it off, resulting in a failure to run, I found a large half day. In addition, you can check the "system log" to see what the original
Cause the operation to fail.

Okay, so much for the mission plan application, now let's cut to the chase, and here are two examples:

First, let PHP run regularly

Edit the following code and save as test.php:
Copy the Code code as follows:

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

To add a task schedule, enter the command in the Step (2):
Copy the 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 the task.
Now let's see if the content of the D:\php4\test.txt file is successful. If the content is as follows, congratulations on your success.

Copy the Code code as follows:
2003-03-03 11:08:01 let PHP run it regularly!
2003-03-03 11:09:02 let PHP run it regularly!
2003-03-03 11:10:01 let PHP run it regularly!
2003-03-03 11:11:02 let PHP run it regularly!

Second, let MySQL achieve automatic backup

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

Copy the Code code 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) Ptker All rights reserved.
This was free Software,and Welcome to modify and redistribute it
Under the GPL license

PHP Shell script for the backup MySQL database.

Usage:

Can is database name would 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 $dbname. rar $dbname. sql");
@unlink ("$dbname. sql");
echo "Backup complete!";
}
?>

To add a task schedule, enter the command in the Step (2):
Copy the 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 the task.
Finally, a RAR file consisting of the database name and the current time is generated under the D:\php4\ directory.
Congratulations to you! It's done!
Of course there are many ways of backup, readers can do as they like!

The above is the original. Combined with my real base, add the following:

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

On the top ' "4. The system will then 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, the password can be empty.
This system is very high, higher than your administrator, so you do not want to run the command, this is no hint will be unconditional execution, this permission under your kill core process is OK.

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

Copy the Code code as follows:
D:\php4\php.exe-q D:\php4\test.php

The correct form should be
Copy the Code code as follows:
"D:\php4\php.exe"-Q "D:\php4\test.php"

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

Recently made a few PHP game projects, there are chess games also have RPG games, which are more or less need some regular update information mechanism. such as chess game player Timeout detection. RPG games in the use of more, monster refresh, automatic back-up, task expiration, leaderboard refresh and so on. Because PHP does not have a memory-resident program, there are some difficulties in handling it.

I refer to the implementation of some peers, the usual practice is based on the needs of specific projects, C + +, Python, Java and other write an auxiliary program, timed to update the database. But
It is very troublesome to do so. First of all, these auxiliary programs need to understand the other language programmer intervention, will inevitably increase the development cost and risk. Second, the differences between programmers in different languages are cumbersome and slow to
The relationship between the auxiliary program and the foreground is very close, basically need to develop at the same time, debugging together.

I used a project in the implementation of a timed task of the method, I feel that this scheme is better, belong to once and for all, all the code to the PHP side.

First, in the database, define a table called task, which has two fields Exectime and
Url. Where Exectime is a UNIX type of time, the URL is string-type. Each piece of data represents a task, meaning "This task is executed at Exectime,
The address to execute is URL ". The auxiliary program monitors the table every second, compares the current time to each task in the table, requests the URL if the time is reached, and then the task executes and deletes
This task. So cyclical.

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

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

Supplemental content:

The task of the opening is like this, we made a similar large online game server switch interface, after logging in the background, to the server control page, you can view the current server running state, you can open or shut down the server. To open the server is to insert related tasks into the task list, shut down the server is to empty the task list. is a manual form.


The task is 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 program, so each task can only be executed once. If a task needs to be executed in a loop, it can only be done by inserting himself into the task list in the PHP code that executes the task (the URL of the task).


Task timeout, the task timeout is divided into two kinds, the data table, the execution time between tasks, one is to request the task page timeout. The first scenario does not occur because the secondary program executes all tasks that are less than or equal to the current time each time. In the second case, the auxiliary program will automatically determine whether the access to the page is successful, if the return server is wrong or cannot connect, and so on, keep this task, do not delete, wait until the next cycle to try to execute.

http://www.bkjia.com/PHPjc/767613.html www.bkjia.com true http://www.bkjia.com/PHPjc/767613.html techarticle The so-called task plan is the computer automatically call the user pre-set application, so as to simplify the purpose of user operations. Take advantage of Windows 2000 Task Scheduler (quite with ...

  • 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.