cPanel';s cron作业因未知原因创建PHP文件的别名。。。?

cPanel';s cron作业因未知原因创建PHP文件的别名。。。?,php,cron,duplicates,alias,cpanel,Php,Cron,Duplicates,Alias,Cpanel,我有cron作业设置,经常运行一些PHP脚本。问题在于,每次运行脚本时,它都会创建一个别名,或者创建一个具有相同文件名并在末尾添加数字的空文件 例如,其中一个文件是activesessions_update.cron.php,下面是其中的脚本: <?php $memcache = new Memcache; $memcache->connect('127.0.0.1', 11211); $activeSessions = $memcache->getStats()

我有cron作业设置,经常运行一些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);
?>
我不知道是什么问题。我被迫每周删除10000个这些空文件。请注意,我在PHP编程方面没有经验,因为我没有自己编写代码,因此任何回复都将非常详细。谁能指导我解决这个难题

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

它没有准确地记录日志,默认情况下,wget用于下载文件。 因此,当您针对该url运行wget时,它会发出请求文件, 并下载请求的输出,本质上是保存 如果你打开浏览器,你会在浏览器中看到什么。通过添加 -O/dev/null,而不是保存 输出到默认位置(通常在它所在的任何位置) 从中调用)以将其保存到/dev/null,该文件实际上不存在 (基本上就是把它扔掉)


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

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

因为我们已经确定这些是由cron创建的日志文件,解决方法是让PHP脚本在运行时删除日志文件。将
activesessions\u 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创建的日志文件,一个解决办法是让PHP脚本在运行时删除日志文件。将
activesessions\u 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);

?>


我已经在cPanel中禁用了日志记录,因此它不是日志文件。所有的文件都是空的…@Johan尝试将这一行(在它自己的一行上)添加到
activesessions\u update.cron.php
文件中,紧接着
我已经在cPanel中禁用了日志记录,所以它不是日志文件。所有的文件都是空的…@Johan尝试将这一行(在它自己的一行上)添加到
activesessions\u update.cron.php
文件中,紧跟在
之后,您似乎(至少)有两台服务器参与其中-cron作业的预定服务器,以及脚本在其上执行的脚本-除非您回拨到自己的服务器。在哪一个上创建重复文件?另外,注释中说这将是一个用户定义的函数,这正是文件中的内容,还是用
file\u put\u contents()替换的用户定义函数
就本文而言?您可以尝试将
/dev/null
添加到cron命令的末尾,以避免日志输出。@Daverandom我正在回拨到自己的服务器。请注意,cron作业的所有文件都会创建这种类型的文件。@ldg请详细说明一下。我会这样添加它吗:/usr/bin/wget/dev/null?必须说这是一种奇怪的方式来完成这项工作。您正在通过HTTP调用本地脚本,这会增加一些处理开销,这意味着很难捕获错误。最好将调用的命令更改为
php/actual/local/path/to/activesessions\u update.cron.php
这里似乎涉及(至少)两台服务器,一台是cron作业调度的服务器,另一台是脚本执行的服务器,除非您回叫到自己的服务器。在哪一个上创建重复文件?另外,注释中说这将是一个用户定义的函数,这正是文件中的内容,还是用
file\u put\u contents()替换的用户定义函数
就本文而言?您可以尝试将
/dev/null
添加到cron命令的末尾,以避免日志输出。@Daverandom我正在回拨到自己的服务器。请注意,cron作业的所有文件都会创建这种类型的文件。@ldg请详细说明一下。我会这样添加它吗:/usr/bin/wget/dev/null?必须说这是一种奇怪的方式来完成这项工作。您正在通过HTTP调用本地脚本,这会增加一些处理开销,这意味着很难捕获错误。最好将调用的命令更改为
php/actual/local/path/to/activesessions\u update.cron.php
我是否将新行添加到cron作业运行的其他php文件中?我现在将对此进行测试,并在几分钟内报告……如果存在权限问题,可以设置另一个cron来删除这些文件,可能频率较低。@Johan yes您应该能够将其添加到您想要的任何文件中,只要它们满足两个条件(请参阅我编辑的答案底部的注释)它不起作用。刚刚看到另一个日志文件。虽然我应该注意到日志文件最终在目录:/home/USER/中,而php文件在:/home/USER/public\u html/x/activesessions\u update.cron.php中,但我不知道这是否会改变什么,我想我应该记下来…@J