Crontab不会使用PHP从FTP服务器下载文件

时间:2013-04-18 15:51:55

标签: php ftp crontab

我有一个PHP脚本,我已写入登录FTP服务器,下载抑制数据的csv文件,并将其与我们当前的抑制列表文件合并。

从命令行运行它时效果很好。当我将它设置为每天在crontab中运行一次时,它总是说它无法从FTP服务器下载文件,并退出。我给了脚本写入文件的目录777,所以我无法想象这将是一个权限问题。

还有其他人有这个问题吗?我有什么简单的东西吗?下面是我的crontab行和我的脚本代码,如果这有助于给出答案。谢谢!

Crontab条目:

0 8 * * * php /var/www/scripts/ftp-unsubs.php >> /var/www/scripts/logs/ftp-unsubs.log

PHP脚本:

<?
// get latest suppression file and append to our global suppression file (loader.txt)

$yesterday = date("Y-m-d", strtotime("yesterday"));
$ftp_server = '--hidden--';
$ftp_user_name = '--hidden--';
$ftp_user_pass = '--hidden--';
$local_file = 'unsub_dls/unsubs.' . $yesterday . '.csv';
$remote_file = 'FJM378_unsubs.' . $yesterday . '.csv';


// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

ftp_pasv($conn_id, TRUE);

print "\n[" . date("r") . "]\n";

if ((!$conn_id) || (!$login_result)) {
    echo "FTP connection has failed!\n";
    echo "Attempted to connect to $ftp_server for user $ftp_user_name\n";
    exit;
} else {
    echo "Connected to $ftp_server, for user $ftp_user_name\n";
}

// try to download $remote_file and save to $local_file

if (ftp_get($conn_id, $local_file, $remote_file, FTP_BINARY)) {
  echo "Successfully written to $local_file\n";
} else {
  echo "Couldn't get file $remote_file\n";
}

// close the FTP stream
if (ftp_close($conn_id)) {
  print "Closed the connection to $ftp_server\n";
}


// grab new unsubs and add timestamp
print "\nProcessing file $local_file\n";

if (file_exists($local_file)) {

  $fh = fopen($local_file, 'r');

  $i = 0;
  $unsub = "";
  while (!feof($fh)) {
    $line = fgets($fh);
    $line = preg_replace('/(?:(?:\r\n|\r|\n)\s*){2}/s', "", $line);
    if ($line != "" && $line != "email") {
      $unsub .= $line . "," . date("Y-m-d H:i:s") . "\n";
      $i++;
    }
  }

  fclose($fh);

  // add unsubs to global unsub file
  $fsupp = fopen('loader.txt','a');
  fwrite($fsupp, $unsub);
  fclose($fsupp);

  print "Added $i unsubs to loader.txt\n\n";

} else {
  print "Error: file '$local_file' doesn't exist! Bailing.\n\n";
}
?>

1 个答案:

答案 0 :(得分:0)

尝试将此任务应运行的用户名添加到您的crontab条目中,如下所示:

0 8 * * * root php /var/www/scripts/ftp-unsubs.php&gt;&gt; /var/www/scripts/logs/ftp-unsubs.log