Cron 不创建文件
使用以下代码,我使用从数据库获取的信息创建一个 xml 文件:
<?php
//include 'config.php';
include '/var/www/html/folder/config.php';
$now=date('Y-m-d h:i:s');
echo "Date: ".$now."<br><br>";
$sql="SELECT * FROM awards WHERE active=3";
$result=mysql_query($sql);
// create doctype
$dom = new DOMDocument("1.0");
// create root element
$root = $dom->createElement("data");
$dom->appendChild($root);
$dom->formatOutput=true;
while($data=mysql_fetch_array($result)){
echo $data['title'];
// create ITEM
$item = $dom->createElement("item");
$root->appendChild($item);
// ID DOM
$subitem = $dom->createElement("id");
$item->appendChild($subitem);
$text = $dom->createTextNode($data['id']);
$subitem->appendChild($text);
// title DOM
$subitem = $dom->createElement("title");
$item->appendChild($subitem);
$text = $dom->createTextNode($data['title']);
$subitem->appendChild($text);
}
if(unlink ("api/2.xml")){
echo "deleted<br>";
}
if($dom->save("api/2.xml")){
echo "created";
}
?>
这工作没有问题,当我手动执行它时,会创建文件 2.xml。
但是,当我将其添加到 crontab 时,日志显示 cron 正在执行(我获得了脚本开头回显的日期以及 while 循环内回显的标题),但未创建 2.xml 文件。
有什么线索为什么它没有创建吗?
With the following code I'm creating a xml file with the info obtained from my database:
<?php
//include 'config.php';
include '/var/www/html/folder/config.php';
$now=date('Y-m-d h:i:s');
echo "Date: ".$now."<br><br>";
$sql="SELECT * FROM awards WHERE active=3";
$result=mysql_query($sql);
// create doctype
$dom = new DOMDocument("1.0");
// create root element
$root = $dom->createElement("data");
$dom->appendChild($root);
$dom->formatOutput=true;
while($data=mysql_fetch_array($result)){
echo $data['title'];
// create ITEM
$item = $dom->createElement("item");
$root->appendChild($item);
// ID DOM
$subitem = $dom->createElement("id");
$item->appendChild($subitem);
$text = $dom->createTextNode($data['id']);
$subitem->appendChild($text);
// title DOM
$subitem = $dom->createElement("title");
$item->appendChild($subitem);
$text = $dom->createTextNode($data['title']);
$subitem->appendChild($text);
}
if(unlink ("api/2.xml")){
echo "deleted<br>";
}
if($dom->save("api/2.xml")){
echo "created";
}
?>
This is working with no problem, file 2.xml is created, when I execute it manually.
But when I add it to the crontab the log shows that the cron is being executed (I obtain the date echoed at the beginning of the script and also the title echoed inside the while loop) but the 2.xml file is not created.
Any clues why is it not created?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将脚本迁移到
cron
,那么您总是需要检查两件事:root
并不能解决所有问题)。我们无法为您检查文件权限,但我可以告诉您,您正在使用隐式路径,该路径很可能无法以这种形式工作:
您现在有文件夹
api
浮动在你的文件系统中的某个地方。使用绝对路径就可以了。If you migrate a script to
cron
than you always need to check two things:root
is not the solution to everything).We can't check the file permissions for you, but I can tell you that you're using implicit paths which, most likely, can not work in that form:
You now have the folder
api
floating around somewhere in your filesystem. Use absolute paths and you're good to go.