Home Backend Development PHP Tutorial windows定时执行PHP的技能

windows定时执行PHP的技能

Jun 13, 2016 pm 12:29 PM
nbsp php windows

windows定时执行PHP的技巧

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文件的内容时候是否成功。如果内容为如下所示,那么恭喜你成功了。

2016-03-03 11:08:01 让PHP定时运行吧!
2016-03-03 11:09:02 让PHP定时运行吧!
2016-03-03 11:10:01 让PHP定时运行吧!
2016-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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks 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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

Four ways to implement multithreading in C language Four ways to implement multithreading in C language Apr 03, 2025 pm 03:00 PM

Multithreading in the language can greatly improve program efficiency. There are four main ways to implement multithreading in C language: Create independent processes: Create multiple independently running processes, each process has its own memory space. Pseudo-multithreading: Create multiple execution streams in a process that share the same memory space and execute alternately. Multi-threaded library: Use multi-threaded libraries such as pthreads to create and manage threads, providing rich thread operation functions. Coroutine: A lightweight multi-threaded implementation that divides tasks into small subtasks and executes them in turn.

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

Why can't my code get the data returned by the API? How to solve this problem? Why can't my code get the data returned by the API? How to solve this problem? Apr 01, 2025 pm 08:09 PM

Why can't my code get the data returned by the API? In programming, we often encounter the problem of returning null values ​​when API calls, which is not only confusing...

Where to download Python .whl files under Windows? Where to download Python .whl files under Windows? Apr 01, 2025 pm 08:18 PM

Python binary library (.whl) download method explores the difficulties many Python developers encounter when installing certain libraries on Windows systems. A common solution...

How to monitor system performance through Debian logs How to monitor system performance through Debian logs Apr 02, 2025 am 08:00 AM

Mastering Debian system log monitoring is the key to efficient operation and maintenance. It can help you understand the system's operating conditions in a timely manner, quickly locate faults, and optimize system performance. This article will introduce several commonly used monitoring methods and tools. Monitoring system resources with the sysstat toolkit The sysstat toolkit provides a series of powerful command line tools for collecting, analyzing and reporting various system resource metrics, including CPU load, memory usage, disk I/O, network throughput, etc. The main tools include: sar: a comprehensive system resource statistics tool, covering CPU, memory, disk, network, etc. iostat: disk and CPU statistics. mpstat: Statistics of multi-core CPUs. pidsta

See all articles