Home  >  Article  >  Backend Development  >  Windows executes PHP regularly

Windows executes PHP regularly

小云云
小云云Original
2018-03-10 10:28:183083browse

I believe that many readers (PHP enthusiasts) often complain during the process of work and study: How to make PHP automatically send emails at regular intervals in WIN? ? How to make MYSQL realize automatic backup without worries? ? Of course it can be done entirely manually, but the operation seems a bit too cumbersome! Don't worry, this problem can be easily solved by using the system's task scheduler (called a scheduled task in Windows 98). This feature is often overlooked or never thought of by many users :).

The 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. Using the task scheduler of Windows 2000 (equivalent to the cron program under *NIX, which will not be described in detail here), we can arrange for any script, program or document to run at the most appropriate time to meet our own 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:

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

2. 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 desired program from the given program list. Automatically run the application and 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.

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 If the relevant conditions match 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 can be scheduled on time. implement).

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 be started 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 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!
1. 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") . " 让PHP定时运行吧!/n"); 
 fclose($fp); 
 ?>

2. Add a task plan and enter the command in this step (as shown in Figure 2): D:/php4 /php.exe -q D:/php4/test.php

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

4. 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.

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. Make it possible for MYSQL to realize automatic backup!
1. Edit the following code and save it as backup.php. If you want to compress it, you can copy a rar.exe:

<?php 
 if ($argc != 2 || in_array($argv[1], array(&#39;--help&#39;, &#39;-?&#39;))) { 
 ?>
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!"; 
 } 
 ?>

2、添加一个任务计划,在(如图2所示)这一步输入命令:
   D:/php4/php.exe -q D:/php4/backup.php databasename
3、时间设置为每天运行一次,然后运行这个任务。
4、最后会在d:/php4/目录下生成一个以数据库名和当前时间组成的rar文件。
5、恭喜你!大功告成了!
    当然备份方式有很多种,读者可按照自己喜欢的去做!
以上是原著.结合本人实贱,补充说明如下:

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

在上面'"4.接下来系统将会要求用户设置适当的用户名及密码(如图5所示),以便系统今后能自动加以运行".这里最好用"system"用户,密码可为空.如图:

Windows executes PHP regularly

这个system的权限非常之高,比你的administrator还要高,所以你在运行命令的时候千万不要乱来,这个可是什么提示都没有就会无条件执行的,这个权限下你kill核心进程都行.

2.上面'"'2、添加一个任务计划,在(如图2所示)这一步输入命令:D:/php4/php.exe -q D:/php4/test.php",正确形式应为  '"D:/php4/php.exe'" -q '"D:/php4/test.php'"  ,即路径要用双引号括住.

相关推荐:

window下如何实现定时执行php脚本

windows定时执行PHP的技巧 windows10 下载 windows优化大师 windows7 64位旗舰

Linux下crontab定时执行PHP程序的方法

The above is the detailed content of Windows executes PHP regularly. For more information, please follow other related articles on the PHP Chinese website!

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