The so-called task planning is that the computer automatically calls the application set by the user in advance, so as to simplify user operations. Using Windows 2000's task scheduler (which is equivalent to the cron program under *NIX, which will not be described in detail here), we can arrange for any script, program or document to run at the most appropriate time to meet our needs. Here is a case in Windows 2000.
Specifically, if we need to use the task scheduler to run automatically, we should perform the following steps:
Click the Start button, and then select Programs → Attachments → System Tools → Task Schedule (or Settings → Control Panel → Task Schedule) to start the task schedule management program for Windows 2000.
In the Task Schedule window, double-click the Add Task Schedule icon, launch the system's Task Schedule Wizard, and then click the Next button, select the application that you want to run automatically in the list of programs given, and then click the Next button.
Set the 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.), and then click the "Next" button.
At this time, the system will require users to set the specific time for the program to run, such as what date, what time period to run, etc. We only need to set it according to our needs.
Next, the system will require 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 just click the "Finish" button to add the corresponding task to Windows
In the 2000 task scheduler, it will automatically "remember" this task after that. Once the system time and related conditions match the user's plan, it will automatically call the application specified by the user, which is very small.
(Every time you start Windows 2000, the task scheduler will automatically start and run in the background to ensure that the user's plan can be executed on time).
Now let’s test whether the task we just built is successful. Right-click the “php” program icon (as shown in Figure 6) and select “Run” in the pop-up menu. Generally, the program icon is the same
Activate and run to start normally. If the run fails, you can check whether the user and password are set correctly, and you can also confirm "Task
Whether the Scheduler service has been started, I turned it off to save system resources, causing the operation to fail, which made me search for it for most of the day. In addition, you can also check what it is from the "system log"
Due to the failure of operation.
Okay, after talking about so many tasks planning applications, now we get to the point, and we will introduce two examples below:
1. Let PHP run regularly
Edit the following code and save it as:
<?php
$fp = @fopen("", "a+");
fwrite($fp, date("Y-m-d H:i:s") . "Let PHP run timed!\n");
fclose($fp);
?>
Add a task plan and enter the command in this step (as shown in Figure 2):
D:\php4\ -q D:\php4\
The time is set to run every 1 minute, and then run this task.
Now let’s see if the content of the d:\php4\ file is successful. If the content is as follows, then congratulations on your success.
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 achieve automatic backup
Edit the following code and save it as, if you want to compress it, you can copy one:
<?php
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: <?php echo $argv[0]; ?> <option>
<option> can be 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 > ./$");
@exec("$rar_tool a -ag_yyyy_mm_dd_hh_mm $ $");
@unlink("$");
echo "Backup complete!";
}
?>
Add a task plan and enter the command in this step (as shown in Figure 2):
D:\php4\ -q D:\php4\ databasename
The time is set to run once a day and then run this task.
Finally, a rar file composed of the database name and current time will be generated in the d:\php4\ directory.
congratulations! The task is done!
Of course, there are many ways to backup, readers can do it as they like!
The above is the original work. In combination with my real low profile, the following additional explanation is as follows:
If an error occurs:
An error occurred while trying to set task account information
The specified error is:
0x80070005: Access denied
You do not have permission to run the requested operation
'"4. Next, the system will require the user to set the appropriate username and password so that the system can run automatically in the future. "This is best to use "system" users, and the password can be empty.
The permissions of this system are very high, even higher than your administrator, so don't do it randomly when running commands. However, if there is no prompt, you will execute unconditionally. With this permission, you can kill the core process.
2. Add a task plan above, enter the command in this step:
D:\php4\ -q D:\php4\
The correct form should be
"D:\php4\" -q "D:\php4\"
That is, the path should be enclosed in double quotes.
I have recently done several PHP game projects, including chess and card games and rpg games, all of which require more or less some mechanisms to update information regularly. For example, players in chess and card games will be timed out. More are used in rpg games, such as monster refresh, automatic blood recovery, mission expiration, ranking refresh, etc. Because PHP does not have a memory resident program, there are some difficulties in processing.
I have referred to some implementation methods of peers. The usual practice is to write an auxiliary program in c++, python, java, etc. according to the needs of specific projects, and update the database regularly. but
It's very troublesome to do this. First of all, these auxiliary programs require the intervention of programmers who understand another language, which will inevitably increase certain development costs and risks. Second, it is very troublesome to coordinate the programmers of different languages, and the progress is very slow.
The relationship between the auxiliary program and the foreground is very close, and it basically requires development and debugging at the same time.
I used a method of executing tasks regularly in my project. I felt that this solution was better and was a one-time and for all. I handed over all the code to PHP.
First, in the database, define a table named task, with two fields executed and
url. where execute is a Unix type time, and url is string type. Each piece of data represents a task, and the specific meaning is "this task is executed during execution time,
The execution address is url". The auxiliary program will monitor this table every second, comparing the time of each task in the table. If the time is reached, request the url, and then the task execution is completed and deleted
This task. This cycle goes on and on.
The advantage of this is that PHP program developers are free to execute the web pages they want at the time they want. Moreover, this program only needs to be written once and can be used well in any similar project.
I made this program into a Daemon for Windows Service and archlinux, which realized the cross-platform of the entire project.
Supplementary content:
The task is opened like this. We have created a server switch interface similar to a large online game. After logging in to the game background, we go to the server control page to view the current server's running status and enable or close the server. Turning on the server is to insert the relevant tasks into the task list, and turning off the server is to clear the task list. It is in artificial form.
Tasks are repeatedly enabled, because these tasks are inserted into the task table by php, and each task in the task table is deleted by the auxiliary program after execution, so each task can only be executed once. If there is a task that needs to be executed loopfully, it can only be inserted into the task list by reinserting it into the php code that executes this task (i.e., the url of the task).
There are two types of timeouts for a task. In the data table, the timeout between executions of tasks is timed out. One is to request that the task page timeout. The first case does not occur because the auxiliary program performs all tasks less than or equal to the current time each time. In the second case, the auxiliary program will automatically determine whether the access to this page is successful. If the server error is returned or the connection cannot be connected, the task will be retained and not deleted. Wait until the next loop is attempted.