当前位置:asp编程网>技术教程>Linux教程>  正文

nginx服务器定时运行thinkphp开发的功能

2018-05-14 13:38:08   来源:www.aspbc.com   作者:wangsdong   浏览量:3417   收藏

功能:在nginx服务器上定时运行thinkphp开发的一个功能

来源:www.aspbc.com

注意:原创文章,欢迎转载,转载请注明来源asp编程网,谢谢


首先:我们在thinkphp的控制器中添加一个函数log()
函数功能:先获取服务器当前时间,然后将时间追加到newfile.txt中,代码如下:
public function log(){
   $path = dirname(dirname(dirname(dirname(__FILE__))))."/log1/newfile.txt";
   $myfile = fopen($path, "a+") or die("Unable to open file!");
   $txt = date("Y-m-d H:i:s")."\n";
   fwrite($myfile,$txt);
   fclose($myfile);
}


其次:我们使用xshell,连接上nginx服务器,输入


[root@aaa]# crontab -e

回车,打开编辑框,在里面输入
*/1 * * * * wget http://localhost/index/log.html  --每隔1分钟运行一次

10 13 * * * wget http://localhost/index/log.html  --每天13:10分运行一次


然后输入:wq保存


这样就完成了nginx定时运行thinkphp开发的功能了。当时间到达后,你去当前目录下log1文件夹下看看有没有newfile.txt文件。如果有这个文件,说明已经定时运行了thinkphp功能了。

不过要注意一点:像第一种,每隔1分种运行一次,不太受到影响;但第二种,定在13:10分运行这种,可能因为nginx服务器时间和个人电脑上的时间不一致,看不到newfile.txt文件。



关于我们-广告合作-联系我们-积分规则-网站地图

Copyright(C)2013-2017版权所属asp编程网