Home  >  Article  >  Backend Development  >  PHP program runs automatically, windows scheduled tasks_PHP tutorial

PHP program runs automatically, windows scheduled tasks_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:12:23878browse

In windows, if you want the PHP program to run automatically, then we must use the windows scheduled task to complete it. Let me introduce the implementation method to you.

Specifically, if we need to use the task scheduler to run automatically, we should perform the following steps:

1. Click the "Start" button, and then select "Programs" → "Accessories" → "System Tools" → "Task Scheduler" (or "Settings" → "Control Panel" → "Task Scheduler") to start the Windows 2000 task Program management procedures.

2. Double-click the "Add Task Schedule" icon in the "Task Schedule" window to start the system's "Task Schedule Wizard", then click the "Next" button, select the application that needs to run automatically in the given program list, and then click Click the "Next" button. ​

3. Set an 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 ask the user to set the specific time for the program to run, such as what day, what time, what time period it can run, etc. We only need to set it according to our own needs. ​

4. Next, the system will ask the user to set an appropriate username and password (as shown in Figure 5) so that the system can run automatically in the future. ​

5. Finally, we only need to click the "Finish" button to add the corresponding task to the Windows 2000 Task Scheduler. After that, it will automatically "remember" the task once the system time and related conditions match the schedule set by the user. , it will automatically call the application specified by the user, which is very convenient (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 created is successful. Right-click the “php” program icon (as shown in Figure 6) and select “Run” in the pop-up menu. Under normal circumstances, the program icon can start normally as long as it is activated in this way. If the operation fails, you can check whether the user and password are set correctly, and confirm whether the "Task Scheduler" service has been started. I turned it off in order to save system resources, which caused the operation to fail, which cost me a long time to find. In addition, you can also check the "System Log" to see what caused the operation to fail.

Okay, after talking about so many tasks planning applications, let’s get to the point. Two examples will be introduced below:

1. Let PHP run regularly

Edit as follows code and save it as test.php:

$fp = @fopen("test.txt", "a+");
fwrite($fp, date("Y-m-d H:i:s" ) . "Let PHP run regularly!/n");
fclose($fp);
?>

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

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

Set the time to run every 1 minute, and then run this task. Now let's see if the content of the d:/php4/test.txt file is successful. If the content is as shown below, congratulations on your success.

2007-10-30 11:08:01 Let PHP run regularly!
2007-10-3011:09:02 Let PHP run regularly!
2007-10-30 11:10:01 Let PHP run regularly!
2007-10-30 11:11:02 Let PHP run regularly!

2. Let MYSQL realize automatic backup

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

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 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!";
}
?>

Add a task Plan, 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 every day Run it once and then run the task. Finally, a rar file consisting of the database name and the current time will be generated in the d:/php4/ directory. congratulations! You're done!

Of course, there are many backup methods, readers can do what they like!

The above is the original work. Based on my own experience, the additional explanation is as follows:

If an error occurs:

An error occurs when trying to set task account information
The specified error is:
0×80070005: Access Denied
You do not have permission to run the requested operation

above'" 4. Next, the system will ask the user to set an appropriate username and password so that the system It can be run automatically in the future." It is best to use the "system" user here, and the password can be empty.
The permissions of this system are very high, higher than your administrator, so you must not run the command No matter what, this will be executed unconditionally without any prompts. With this permission, you can kill the core process.

2. Add a task plan and enter the command at this step:

D:/php4/php.exe -q D:/php100/test.php

The correct form should be

“D:/php4/php.exe” -q “D:/ php100/test.php”

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


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444580.htmlTechArticleIn windows, if you want the PHP program to run automatically, then we must use windows scheduled tasks to complete it. Below I Let me introduce to you how to implement it. Specifically, if we need...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn