search
HomeBackend DevelopmentPHP TutorialExamples of using scheduled tasks to automatically execute PHP programs in Windows_PHP Tutorial

The so-called task plan is for the computer to automatically call the applications set by the user in advance, thereby simplifying the user's operations. Using the task scheduler of Windows 2000 (equivalent to the cron program under *NIX, which will not be described in detail here), we can schedule any script, program or document to run at the most appropriate time to meet our needs. The following takes Windows 2000 as an example.

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

Click the "Start" button, then select "Programs" → "Accessories" → "System Tools" → "Task Scheduler" (or "Settings" → "Control Panel" → "Task Scheduler") to start Windows 2000 task planning management program.
Double-click the "Add Task Plan" icon in the "Task Plan" window to start the system's "Task Plan Wizard", then click the "Next" button and select the application that needs to be run automatically in the given program list , and then click the "Next" button.
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.), then click "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.
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.
Finally, we only need to click the "Finish" button to add the corresponding task to the task scheduler of Windows
2000. After that, it will automatically "remember" the task once the system time and related conditions If it matches the plan 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 able to execute 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 and running. 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 applications of task planning, let’s get to the point. Two examples will be introduced below:

1. Let PHP run regularly

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


Copy the code The code is as follows:
$fp = @fopen("test.txt", "a+");
fwrite($fp, date("Y-m-d H:i:s") . " Let PHP Run it regularly! n");
fclose($fp);
?>
Add a task plan and enter the command at this step (as shown in Figure 2):


Copy the code The code is as follows:
D:php4php.exe -q D:php4test.php

Set the time to run every 1 minute and then run this task.

Now let’s see if the content of the d:php4test.txt file was successful. If the content is as shown below, congratulations on your success.

Copy code The code is 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!
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:

Copy code The code is 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) 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:


添加一个任务计划,在(如图2所示)这一步输入命令:
复制代码 代码如下:

D:php4php.exe -q D:php4backup.php databasename

时间设置为每天运行一次,然后运行这个任务。
最后会在d:php4目录下生成一个以数据库名和当前时间组成的rar文件。
恭喜你!大功告成了!
当然备份方式有很多种,读者可按照自己喜欢的去做!

以上是原著.结合本人实贱,补充说明如下:

如果出现错误:


在试着设置任务帐户信息时出现错误
指定的错误是:
0x80070005:拒绝访问
您没有运行所请求的操作的权限

在上面'"4.接下来系统将会要求用户设置适当的用户名及密码,以便系统今后能自动加以运行".这里最好用"system"用户,密码可为空.
这个system的权限非常之高,比你的administrator还要高,所以你在运行命令的时候千万不要乱来,这个可是什么提示都没有就会无条件执行的,这个权限下你kill核心进程都行.

上面2、添加一个任务计划,在这一步输入命令:

复制代码 代码如下:

D:php4php.exe -q D:php4test.php

正确形式应为

复制代码 代码如下:

"D:php4php.exe" -q "D:php4test.php"

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

I have recently made several PHP game projects, including chess and card games and RPG games, all of which more or less require mechanisms for regularly updating information. For example, player timeout detection in chess and card games. It is used more in RPG games, such as monster refresh, automatic blood recovery, task expiration, ranking refresh, etc. Because PHP does not have memory-resident programs, there are some difficulties in processing.

I referred to the implementation methods of some peers. The usual approach is to write an auxiliary program in c++, python, java, etc. to update the database regularly according to the needs of the specific project. But
it is very troublesome to do so. First of all, these auxiliary programs require the intervention of programmers who know another language, which will inevitably increase development costs and risks. Second, joint debugging between programmers of different languages ​​is very troublesome and the progress is very slow. Since the relationship between the auxiliary program and the front desk is very close, they basically need to be developed and debugged together.

I adopted a method of regularly executing tasks in the project. I feel that this solution is better, it is a once-and-for-all type, and all the code is handed over to PHP.

First, define a table named task in the database, which has two fields exectime and

url. Among them, exectime is a unix type time, and url is a string type. Each piece of data represents a task, and the specific meaning is "this task is executed during exectime, and the execution address of
is url". The auxiliary program will monitor this table every second, compare the current time with the time of each task in the table, if the time is reached, request the url, then the task execution is completed, and the
task will be deleted. And so on.

The advantage of this is that PHP program developers can freely execute the web pages they want to execute at the time they want. And this program only needs to be written once and can be used well in any similar project.

I made this program into a windows service and archlinux Daemon, thus realizing the cross-platform of the entire project.

Supplementary content:

The task is started like this. We made a server switch interface similar to that of large-scale online games. After logging in to the game backend, go to the server control page and you can check the running status of the current server and turn the server on or off. Starting the server inserts related tasks into the task list, and closing the server clears the task list. It is artificial form.

Repeated opening of tasks, because these tasks are inserted into the task table by php, and each task in the task table is executed once and then deleted by the auxiliary program, so each task only Can be executed once. If there is a task that needs to be executed in a loop, then the only way is to re-insert itself into the task list in the PHP code that executes the task (that is, the URL of the task).

Task timeout. Task timeout is divided into two types. In the data table, there is a timeout between task executions. One is the timeout when requesting the task page. The first case does not happen because the helper program executes all tasks up to and including the current time each time. In the second case, the auxiliary program will automatically determine whether the access to this page is successful. If a server error is returned or the connection cannot be made, the task will be retained without deletion and will be tried again in the next cycle.

http://www.bkjia.com/PHPjc/767613.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/767613.htmlTechArticleThe so-called task plan is to automatically call the application program set by the user in advance by the computer, thereby simplifying the user's operation. Utilize Windows 2000's Task Scheduler (comparable to...
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
PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python: Code Examples and ComparisonPHP and Python: Code Examples and ComparisonApr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.