cPanel的cron作业创建了PHP文件的别名,原因不明......?

时间:2011-09-04 14:14:56

标签: php cron duplicates alias cpanel

我有cron jobs设置,经常运行一些PHP脚本。问题是,每次运行脚本时,它都会创建一个别名,或者是一个空文件,文件名相同,最后添加一个数字。

例如,其中一个文件是activesessions_update.cron.php,这里面是脚本:

<?php
  $memcache = new Memcache; 
  $memcache->connect('127.0.0.1', 11211);
  $activeSessions = $memcache->getStats(); 

  // using heredoc
$file_content = <<<TEXT
<?php

\$activeSessions = {$activeSessions['curr_items']};

?>
TEXT;

// this would be a user-defined function
file_put_contents("activesessions.php", $file_content);
?>

在我的路径文件夹中,它的外观如下:http://i.imgur.com/kS1JY.png

在cPanel中,cron作业运行命令:

/usr/bin/wget http://domain.com/x/activesessions_update.cron.php

我不知道问题是什么。我被迫每周删除10,000个这些空文件。请注意我没有PHP编程经验,因为我没有自己编写代码,所以任何回复都会非常感激。谁能指导我解决这个难题?

编辑:从主持人的技术支持中获得解决方案:

  

它没有完全记录,默认情况下wget用于下载文件。   所以当你对那个url运行wget时它会消失,请求文件,   并下载请求的输出,基本上保存了一份副本   如果你把它拉起来,你会在浏览器中得到什么。通过添加   -O / dev / null到你告诉它的命令,而不是保存   输出到默认位置(通常在任何地方   调用from)将它保存到/ dev / null,这实际上是无处可去的   (基本上只是抛弃它)

2 个答案:

答案 0 :(得分:1)

由于我们已经确定这些是由cron创建的日志文件,因此解决方法是让PHP脚本在运行时删除日志文件。将activesessions_update.cron.php更改为此 - 首先将原始版本备份到其他目录中!

我假设文件的创建方式使您运行脚本的用户具有删除文件的权限。如果不是,那就不行了。

<?php

  $memcache = new Memcache; 
  $memcache->connect('127.0.0.1', 11211);
  $activeSessions = $memcache->getStats(); 

  // Removed slightly pointless heredoc
  $file_content = "<?php\n\n\  $activeSessions = {$activeSessions['curr_items']};\n\n?>";

  // this would be a user-defined function
  // What does the above comment mean? Are you supposed
  // to replace this with some of your own code?
  file_put_contents("activesessions.php", $file_content);


  // ========================================================
  //     Everything below here is to delete old log files


  // Change this to the directory where the log files end up
  $logsdir = "/home/USER/";

  // Get the name of this file and length of the name
  $filename = basename($_SERVER['PHP_SELF']);
  $namelength = strlen($filename);

  // Strip any trailing slashes from $logsdir
  $logsdir = rtrim($logsdir,'/\\');

  // Open the logs directory
  if (!$dp = opendir($logsdir)) {
    trigger_error("Could not open logs directory '$logsdir' for reading, exiting...");
    exit;
  }

  // Loop through the files in the directory
  while ($file = readdir($dp)) {
    if (!in_array($file,array('.','..',$filename)) && strlen($file) > $namelength && substr($file,0,$namelength) == $filename) {
      // If the start of the file name is the same as this file,
      // and the file name length is longer than the length of the
      // name of this file, delete it.
      @unlink("$logsdir/$file");
    }
  }

  // Close the directory pointer
  @closedir($dp);

?>

我猜你服务器上的cron守护进程被配置为将它运行的所有cron作业的STDOUT和STDERR重定向到一个文件。看起来很奇怪它是这样配置的,因为它会导致你遇到的问题。此外,它应该将它们重定向到基本上与脚本名称相同且末尾带有数字的文件似乎很奇怪。你可能会*.log或其他什么。

此解决方案在任何时候都会保留至少一个日志文件,因为您肯定无法删除当前正在写入的文件。

如果这确实有效,您可以安全地将相同的代码(从=====注释行下方)复制/粘贴到由cron作业调用的任何其他文件中,并导致相同的问题,只要它们可以是:

  • 在脚本所在的同一目录中创建日志文件
  • 目录中的唯一文件(您真正想要的)文件名以脚本文件的全名开头的文件

答案 1 :(得分:0)

我敢打赌,这些是包含脚本输出的日志文件,由cPanel的 cron 创建。检查cPanel的配置并在不需要时禁用日志记录。