Home php教程 php手册 windows定时执行PHP的技巧

windows定时执行PHP的技巧

Jun 21, 2016 am 08:51 AM
php quot windows

windows定时执行PHP相信不少读者(PHP爱好者)在工作、学习的过程中经常抱怨:在WIN如何让PHP定时自动发信呢??如何让MYSQL实现自动备份而无后顾之忧呢??如 果完全依靠手工进行当然也可以实现,但操作上似乎过于繁琐了一点!别着急,利用系统的任务计划程序(Windows 98称之为计划任务)可轻易解决这一问题。这一功能往往被很多用户忽略或者未曾想到:)。

所谓任务计划就是由计算机自动调用用户事先设置好的应用程序,从而达到简化用户操作的目的。利用Windows 2000的任务计划程序(相当与*NIX下的cron程序,这里不再对其详述),我们可以安排任何脚本、程序或文档在最恰当的时候运行,从而满足自己的需 要。下面以Windows 2000为例。

具体来说,我们若需利用任务计划程序自动运行则应执行如下步骤:

1.单击“开始”按钮,然后依次选择“程序”→“附件”→“系统工具”→“任务计划”(或者是“设置”→“控制面板”→“任务计划”),启动Windows 2000的任务计划管理程序。

2.在“任务计划”窗口中双击“添加任务计划”图标,启动系统的“任务计划向导”,然后单击“下一步”按钮,在给出的程序列表中选择需要自动运行的应用程序,然后单击“下一步”按钮。
设置适当的任务计划名称并选择自动执行这个任务的时间频率(如每天、每星期、每月、一次性、每次启动计算机时、每次登录时等),然后单击“下一步”按钮。
此时系统将会要求用户对程序运行的具体时间进行设置,如几号、几点钟、哪几个时间段才能运行等,我们只需根据自己的需要加以设置即可。

4.接下来系统将会要求用户设置适当的用户名及密码,以便系统今后能自动加以运行。

5.最后,我们只需单击“完成”按钮即可将相应任务添加到Windows 2000的任务计划程序中,此后它就会自动“记住”这个任务,一旦系统时间及相关条件与用户设置的计划相符,它就会自动调用用户所指定的应用程序,十分方 便(每次启动Windows 2000的时候,任务计划程序都会自动启动,并在后台运行,确保用户的计划能够按时执行)。

现在我们来测试一下刚才所建的任务是否成功,鼠标右键单击“php”程序图标(如图6所示),在弹出的菜单里面选择“运行”。一般情况下程序图标只要这样 激活运行就可以正常启动。如果运行失败可查看用户和密码是否设置正确,还有确定“Task Scheduler”服务是否已启动,本人当初就是为了节省系统资源把它关掉了导致运行失败,害我找了大半天。另外也可从“系统日志”里查看到底是什么原 因造成运行失败的。

好了,讲了这么多任务计划的应用,现在我们切入正题,下面将介绍两个例子:

一、让PHP定时运行吧!
1、 编辑如下代码,并保存为test.php:
$fp = @fopen("test.txt", "a+");
fwrite($fp, date("Y-m-d H:i:s") . " 让PHP定时运行吧!\n");
fclose($fp);
?>
打开文本输入:D:\php4\php.exe -q D:\php4\test.php
保存为.bat格式。
D:\php4\php.exe 是php安装路径,D:\php4\test.php是要定时运行的程序的路径。

2、添加一个任务计划,选择那个.bat文件

3、时间设置为每隔1分钟运行一次,然后运行这个任务。

4、现在我们来看看d:\php4\test.txt文件的内容时候是否成功。如果内容为如下所示,那么恭喜你成功了。

2003-03-03 11:08:01 让PHP定时运行吧!
2003-03-03 11:09:02 让PHP定时运行吧!
2003-03-03 11:10:01 让PHP定时运行吧!
2003-03-03 11:11:02 让PHP定时运行吧!

二、让MYSQL实现自动备份变成可能!
1、编辑如下代码,并保存为backup.php,如果要压缩可以拷贝一个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. http://www.dareng.com
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:



Statement of this Website
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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) Apr 08, 2025 am 12:03 AM

In PHP, the final keyword is used to prevent classes from being inherited and methods being overwritten. 1) When marking the class as final, the class cannot be inherited. 2) When marking the method as final, the method cannot be rewritten by the subclass. Using final keywords ensures the stability and security of your code.

Explain strict types (declare(strict_types=1);) in PHP. Explain strict types (declare(strict_types=1);) in PHP. Apr 07, 2025 am 12:05 AM

Strict types in PHP are enabled by adding declare(strict_types=1); at the top of the file. 1) It forces type checking of function parameters and return values ​​to prevent implicit type conversion. 2) Using strict types can improve the reliability and predictability of the code, reduce bugs, and improve maintainability and readability.

Solutions to the errors reported by MySQL on a specific system version Solutions to the errors reported by MySQL on a specific system version Apr 08, 2025 am 11:54 AM

The solution to MySQL installation error is: 1. Carefully check the system environment to ensure that the MySQL dependency library requirements are met. Different operating systems and version requirements are different; 2. Carefully read the error message and take corresponding measures according to prompts (such as missing library files or insufficient permissions), such as installing dependencies or using sudo commands; 3. If necessary, try to install the source code and carefully check the compilation log, but this requires a certain amount of Linux knowledge and experience. The key to ultimately solving the problem is to carefully check the system environment and error information, and refer to the official documents.

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

MySQL can't be installed after downloading MySQL can't be installed after downloading Apr 08, 2025 am 11:24 AM

The main reasons for MySQL installation failure are: 1. Permission issues, you need to run as an administrator or use the sudo command; 2. Dependencies are missing, and you need to install relevant development packages; 3. Port conflicts, you need to close the program that occupies port 3306 or modify the configuration file; 4. The installation package is corrupt, you need to download and verify the integrity; 5. The environment variable is incorrectly configured, and the environment variables must be correctly configured according to the operating system. Solve these problems and carefully check each step to successfully install MySQL.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

See all articles