dongsu7049 2014-09-29 20:28
浏览 52

使用cron作业执行PHP脚本

I have a script called cronStats.php that pulls data from Apple's autoingest tool (data on app downloads, region etc) and populates a database for access later on.

When the script is executed in the browser, everything works correctly. But when using a scheduled cron job to execute the script there's an error.

Here is the relevant code:

function pullITCData($username,$password,$VND,$date) {

$fields_string = "USERNAME=" . urlencode($username);
$fields_string .= "&PASSWORD=" . urlencode($password);
$fields_string .= "&VNDNUMBER=" . $VND;
$fields_string .= "&TYPEOFREPORT=Sales";
$fields_string .= "&DATETYPE=Daily";
$fields_string .= "&REPORTTYPE=Summary";
$fields_string .= "&REPORTDATE=$date";  

$fn = "dailyStat_" . $date . "_" . $VND;
$filename = $fn . ".gz";

//$abFN = __DIR__ . "/" . $fn;
//$abFilename = __DIR__ . "/" . $filename;

$abFN = $fn;
$abFilename = $filename;

$ch = curl_init();
echo("<br>abFN url is $abFN");
echo("<br>abFilename url is $abFilename");  
$fp = fopen($abFilename, "w+");

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, 'https://reportingitc.apple.com/autoingestion.tft');
curl_setopt($ch, CURLOPT_POST, 7);
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_FILE, $fp);

//execute post
$contents = curl_exec ($ch);
$fileCreated = false;

if ($contents === false) {
    echo("<br> no contents"); 
} else {
    echo("<br>contents found");
}


fclose($fp);
curl_close($ch);

//delete the opened file
if ($fileCreated == true){
    unlink("$abFN");
}

}

I've removed some code for brevity. Basically, what happens is I pass a string of variables to Apple's ingest tool, which then returns with a .txt.gz file (which is then opened and parsed, not shown here).

When browser executed, I get the "Contents Found" statement, but via Cron Job, the "No Contents" statement meaning that the CURL_EXEC is failing for some reason.

My thinking is that the .GZ file cannot be created because the Cron Job is executing from another directory (?). I tried setting an absolute URL to write the .txt.gz to, but this failed also:

[function.fopen]: failed to open stream: HTTP wrapper does not support writeable connections in

I'm at a loss as to how to proceed. Any ideas?

EDIT: thank you for your feedback, it's appreciated. Based on BojanT's cron command:

/web/cgi-bin/php5 cd "$HOME/html/path/to/php/cron/ && php cronStats.php > cron.log 2>&1

I'm getting the following error:

/bin/sh: -c: line 0: unexpected EOF while looking for matching `"'
/bin/sh: -c: line 1: syntax error: unexpected end of file

EDIT 2 - Work Around: work around method that is working for anyone in a similar situation. It's not elegant / efficient, and doesn't actually solve the issue, but for what it's worth: set up a cron job on another file: e.g. runCron.php:

 <?php

executeScript();

function executeScript() {
file_get_contents("http://website.com/path/to/php/cron/cronStats.php");
informOfExecution();
}

which then executes the actual file at the absolute url. informOfExecution() method sends an email to me notifying of update. it would be nice to cron job the file directly, but done is better than perfect. thanks all.

  • 写回答

1条回答 默认 最新

  • doutizhou5312 2014-09-29 20:44
    关注

    Change to your file's directory with 'cd' and then run your file. Example:

    * * * * * cd /f1/f2/f3/f4/ && php thing.php >> /home/output/outputs.txt
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大