图解任务计划程序的妙用(WIN篇) 不指定

jed , 2006-12-5 11:06 , 服务器技术 , 评论(0) , 阅读(6180) , Via 本站原创 | |
作者:   phpteam,   ptker  
 版权声明:可以任意转载,转载时请务必标明文章原始出处和作者信息及本声明  
   
         相信不少读者(PHP爱好者)在工作、学习的过程中经常抱怨:在WIN如何让PHP定时自动发信呢??如何让MYSQL实现自动备份而无后顾之忧呢??如果完全依靠手工进行当然也可以实现,但操作上似乎过于繁琐了一点!别着急,利用系统的任务计划程序(Windows   98称之为计划任务)可轻易解决这一问题。这一功能往往被很多用户忽略或者未曾想到:)。  
   
         所谓任务计划就是由计算机自动调用用户事先设置好的应用程序,从而达到简化用户操作的目的。利用Windows   2000的任务计划程序(相当与*NIX下的cron程序,这里不再对其详述),我们可以安排任何脚本、程序或文档在最恰当的时候运行,从而满足自己的需要。下面以Windows   2000为例。  
   
         具体来说,我们若需利用任务计划程序自动运行则应执行如下步骤:  
   
 1.单击“开始”按钮,然后依次选择“程序”→“附件”→“系统工具”→“任务计划”(或者是“设置”→“控制面板”→“任务计划”),启动Windows   2000的任务计划管理程序。  
   
 2.在“任务计划”窗口中双击“添加任务计划”图标,启动系统的“任务计划向导”,然后单击“下一步”按钮,在给出的程序列表中选择需要自动运行的应用程序,然后单击“下一步”按钮。  
         设置适当的任务计划名称并选择自动执行这个任务的时间频率(如每天、每星期、每月、一次性、每次启动计算机时、每次登录时等),然后单击“下一步”按钮。  
         此时系统将会要求用户对程序运行的具体时间进行设置,如几号、几点钟、哪几个时间段才能运行等,我们只需根据自己的需要加以设置即可。  
   
 4.接下来系统将会要求用户设置适当的用户名及密码(如图5所示),以便系统今后能自动加以运行。  
   
 5.最后,我们只需单击“完成”按钮即可将相应任务添加到Windows   2000的任务计划程序中,此后它就会自动“记住”这个任务,一旦系统时间及相关条件与用户设置的计划相符,它就会自动调用用户所指定的应用程序,十分方便(每次启动Windows   2000的时候,任务计划程序都会自动启动,并在后台运行,确保用户的计划能够按时执行)。  
   
         现在我们来测试一下刚才所建的任务是否成功,鼠标右键单击“php”程序图标(如图6所示),在弹出的菜单里面选择“运行”。一般情况下程序图标只要这样激活运行就可以正常启动。如果运行失败可查看用户和密码是否设置正确,还有确定“Task   Scheduler”服务是否已启动,本人当初就是为了节省系统资源把它关掉了导致运行失败,害我找了大半天。另外也可从“系统日志”里查看到底是什么原因造成运行失败的。  
   
         好了,讲了这么多任务计划的应用,现在我们切入正题,下面将介绍两个例子:  
   
 一、让PHP定时运行吧!  
 1、       编辑如下代码,并保存为test.php:

&nbsp;<br/><br/> &nbsp;&lt;?php &nbsp; &nbsp; <br/> &nbsp;$fp &nbsp; = &nbsp; @fopen(&quot;test.txt&quot;, &nbsp; &quot;a+&quot;); &nbsp; &nbsp; <br/> &nbsp;fwrite($fp, &nbsp; date(&quot;Y-m-d &nbsp; H:i:s&quot;) &nbsp; . &nbsp; &quot; &nbsp; 让PHP定时运行吧!\n&quot;); &nbsp; &nbsp; <br/> &nbsp;fclose($fp); &nbsp; &nbsp; <br/> &nbsp;?&gt; &nbsp; <br/> &nbsp; &nbsp;<br/>


 2、添加一个任务计划,在(如图2所示)这一步输入命令:D:\php4\php.exe   -q   D:\php4\test.php  
   
 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:  

<br/> <br/> &nbsp;&lt;?php &nbsp; &nbsp; <br/> &nbsp;if &nbsp; ($argc &nbsp; != &nbsp; 2 &nbsp; &#124;&#124; &nbsp; in_array($argv[1], &nbsp; array(&#039;--help&#039;, &nbsp; &#039;-?&#039;))) &nbsp; { &nbsp; &nbsp; <br/> &nbsp;?&gt; &nbsp; &nbsp; <br/> &nbsp;backup &nbsp; &nbsp; Ver &nbsp; 0.01, &nbsp; for &nbsp; Win95/Win98/WinNT/Win2000/WinXP &nbsp; on &nbsp; i32 &nbsp; &nbsp; <br/> &nbsp;Copyright &nbsp; (C) &nbsp; 2000 &nbsp; ptker &nbsp; All &nbsp; rights &nbsp; reserved. &nbsp; &nbsp; <br/> &nbsp;This &nbsp; is &nbsp; free &nbsp; software,and &nbsp; you &nbsp; are &nbsp; welcome &nbsp; to &nbsp; modify &nbsp; and &nbsp; redistribute &nbsp; it &nbsp; &nbsp; <br/> &nbsp;under &nbsp; the &nbsp; GPL &nbsp; license &nbsp; &nbsp; <br/> &nbsp; &nbsp;<br/> &nbsp;PHP &nbsp; Shell &nbsp; script &nbsp; for &nbsp; the &nbsp; backup &nbsp; MySQL &nbsp; database. &nbsp; &nbsp; <br/> &nbsp; &nbsp;<br/> &nbsp;Usage: &nbsp; &lt;?php &nbsp; echo &nbsp; $argv[0]; &nbsp; ?&gt; &nbsp; &lt;option&gt; &nbsp; &nbsp; <br/> &nbsp; &nbsp;<br/> &nbsp; &nbsp; &nbsp;&lt;option&gt; &nbsp; can &nbsp; be &nbsp; database &nbsp; name &nbsp; you &nbsp; would &nbsp; like &nbsp; to &nbsp; backup. &nbsp; &nbsp; <br/> &nbsp; &nbsp; &nbsp;With &nbsp; the &nbsp; --help, &nbsp; or &nbsp; -? &nbsp; options, &nbsp; you &nbsp; can &nbsp; get &nbsp; this &nbsp; help &nbsp; and &nbsp; exit. &nbsp; &nbsp; <br/> &nbsp;&lt;?php &nbsp; &nbsp; <br/> &nbsp;} &nbsp; else &nbsp; { &nbsp; &nbsp; <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$dbname &nbsp; = &nbsp; $argv[1]; &nbsp; &nbsp; <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$dump_tool &nbsp; = &nbsp; &quot;c:\\mysql\\bin\\mysqldump&quot;; &nbsp; &nbsp; <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$rar_tool &nbsp; = &nbsp; &quot;d:\\php4\\rar&quot;; &nbsp; &nbsp; <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@exec(&quot;$dump_tool &nbsp; --opt &nbsp; -u &nbsp; user &nbsp; -ppassword &nbsp; $dbname &nbsp; &gt; &nbsp; ./$dbname.sql&quot;); &nbsp; &nbsp; <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@exec(&quot;$rar_tool &nbsp; a &nbsp; -ag_yyyy_mm_dd_hh_mm &nbsp; $dbname.rar &nbsp; $dbname.sql&quot;); &nbsp; &nbsp; <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@unlink(&quot;$dbname.sql&quot;); &nbsp; &nbsp; <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo &nbsp; &quot;Backup &nbsp; complete!&quot;; &nbsp; &nbsp; <br/> &nbsp;} &nbsp; &nbsp; <br/> &nbsp;?&gt; &nbsp; <br/> &nbsp; &nbsp;<br/>


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


发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]