Cron not creating file
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开发者_如何学Go ("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?
If you migrate a script to cron
than you always need to check two things:
- File permissions, the cron job might get executed with different rights (Reminder:
root
is not the solution to everything). - Implicit paths, the cron job will have a different working directory.
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:
if(unlink("api/2.xml")){
echo "deleted<br>";
}
if($dom->save("api/2.xml")){
echo "created";
}
You now have the folder api
floating around somewhere in your filesystem. Use absolute paths and you're good to go.
精彩评论