Automatically execute PHP program instances using scheduled tasks in Windows

  • 2021-06-28 08:56:44
  • OfStack

The so-called task plan is that the computer automatically invokes the user's pre-set application to simplify the user's operation.With the task scheduler of Windows 2000 (comparable to 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 run the task scheduler automatically, we should perform the following steps:

Click the Start button and start the task plan manager for Windows 2000 by selecting Programs Attachments System Tools Task Schedule (or Settings Control Panel Task Schedule).
In the Task Schedule window, double-click the Add Task Schedule icon, launch the Task Schedule Wizard of the system, click the Next Step button, select the application you want to run automatically in the list of given programs, and then click the Next Step button.
Set the appropriate task schedule name and select the time frequency (e.g., every day, every week, every month, once, every time you start the computer, every time you log on, etc.) to automatically perform this task, and then click the Next Step button.
At this time, the system will require users to set the specific time for the program to run, such as the number, time, which period to run, etc. We only need to set it according to our own needs.
Next, the system will ask 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, we can add the task to Windows simply by clicking the Finish button
In the task scheduler of 2000, it will automatically "remember" the task from now on. Once the system time and related conditions match the schedule set by the user, it will automatically call the application specified by the user, divided into 10 parts
Yes (Every time you start Windows 2000, the task scheduler starts automatically and runs in the background to ensure that the user's schedule can be executed on time).
Now let's test if the task we just built is successful. Right click on the "php" program icon (as shown in Figure 6) and choose Run from the menu that pops up.1 Normally program icons just like this
Activation runs to start normally.If it fails, check to see if the user and password are set correctly, and determine "Task"
Scheduler "Whether the service has started or not, I was looking for half a day to save system resources and shut it down, causing the operation to fail.You can also see what it is from the System Log
Failed to run due to.

Okay, with so many applications of task planning, now let's get to the point. Here are two examples:

1. Let PHP run regularly

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


<?php 
$fp = @fopen("test.txt", "a+"); 
fwrite($fp, date("Y-m-d H:i:s") . "  Give Way PHP Run regularly! \n"); 
fclose($fp); 
?>

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


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

The time is set to run once every minute, then run the task.
Now let's see if the contents of the d:php4test.txt file were successful.If the content is as follows, congratulations on your success.


2003-03-03 11:08:01  Give Way PHP Run regularly!  
2003-03-03 11:09:02  Give Way PHP Run regularly!  
2003-03-03 11:10:01  Give Way PHP Run regularly!  
2003-03-03 11:11:02  Give Way PHP Run regularly! 

2. Let MYSQL do automatic backup

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


<?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 > ./$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 plan and enter the command at this step (as shown in Figure 2):

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

The time is set to run once a day, then run the task.
Finally, an rar file with the database name and current time is generated in the d:php4 directory.
Congratulations!That's it!
Of course, there are many ways to back up, so readers can do what they like!

The above is the original work. In conjunction with my humbleness, the supplementary instructions are as follows:

If an error occurs:


An error occurred while trying to set task account information
The error specified 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 username 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 empty.
This system has very high privileges, which are higher than your administrator, so don't mess up when you run the command, but it will be executed unconditionally without any hints. With this privilege, your kill core processes will work.

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


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

The correct form should be


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

That is, the path is enclosed in double quotes.

Recently, we have done several PHP game projects, including chess games and rpg games, which all need more or less a mechanism to update information regularly.For example, player timeout detection in a board game.rpg games use more, monster refresh, auto-return, task expiration, leaderboard refresh and so on.PHP has some difficulties in processing because it does not have a memory resident.

I have referred to some peer implementations, the usual practice is to write an auxiliary program with c++, python, java, etc. to update the database regularly according to the needs of specific projects.but
That's a hassle.First, these auxiliary programs require the intervention of a programmer who understands another language, which is bound to increase development costs and risks.2. Linking programmers in different languages is cumbersome and slow.
There is a close relationship between the assistant and the foreground, which basically requires simultaneous development and debugging.

I have used a method to perform tasks on time in my project. I feel this is a good solution. It belongs to the one-time-and-never model. I hand over all the code to PHP.

First in the database, you define a table named task with two fields, exectime and
url.exectime is an unix type time and url is a string type.Each data represents a task, meaning "This task is executed at exectime,
The execution address is url.The assistant monitors this table every second, compares the current time to the time of each task in the table, requests the url if the time has elapsed, and then the task is executed and deleted
This task.This loops back and forth.

The advantage of this is that PHP developers are free to 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 well in any similar project.

I made this program an windows service and an archlinux Daemon, which makes the whole project cross-platform.

Supplementary Contents:

The task is opened like this, we have made a server switch interface similar to large online games, logged in to the game background, to the server control page, you can view the current status of the server, you can turn on or off the server.Opening the server inserts related tasks into the task list, closing the server empties the task list.Is a manual form.


Tasks are reopened because they are inserted into the task table by php, and each task in the task table is deleted by the assistant once it is executed, so each task can only be executed once.If a task needs to be executed in a loop, it is only possible to reinsert itself into the task list by executing the php code of the task (that is, the task's url).


There are two types of task timeouts, one is requesting the task page timeout, the other is requesting the task page timeout.The first scenario does not occur because the helper performs all tasks less than or equal to the current time each time.In the second case, the helper automatically determines if the page was accessed successfully. If the server returns incorrectly or cannot be connected, the task is left untouched and will not be deleted until the next loop.


Related articles: