Heim  >  Artikel  >  php教程  >  linux下使用crontab实现定时PHP计划任务失败的原因分析

linux下使用crontab实现定时PHP计划任务失败的原因分析

WBOY
WBOYOriginal
2016-06-13 09:32:25883Durchsuche

很多人在linux下使用crontab实现PHP执行定时任务却未能成功,不能生成缓存。本文就linux下使用crontab实现定时PHP计划任务失败的原因做一分析。

一般我们linux定时执行php代码例如:

*/5 * * * * /usr/local/php/bin/php /home/wwwroot/1.php

其实这样是可以执行php代码的。

但是为什么很多朋友在1.php写的生成一个缓存文件没有生成呢?

这个要涉及到crontab执行php的相对路径问题.

注意在执行文件中,有包含文件的,如:

include_once'./mysql.php'; 

当php代码中,用的是相对路径时,只有进入到那个目录下,

然后执行/usr/local/php/bin/php /home/wwwroot/1.php 才能引用mysql.php生效。

解决方法如下:

*/10 * * * * cd /home/wwwroot && /usr/local/php/bin/php /home/wwwroot/1.php

同时也这样缓存才能生成在 /home/wwwroot。此处需要特别注意!

这是大部分写的所谓linux定时执行php没有提到的,也最容易产生误区的。

当然方法还有很多,还可以设置环境变量之类的。这个只是一个简单的方法。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn