cPanel's cron job creates aliases of PHP file for unknown reason...?
I have cron jobs setup that runs a few PHP scripts often. The issue is that each time it runs a script, it create an alias of it, or an empty file with the same filename and a number added at the end.
For instance, one of the files are activesessions_update.cron.php, here is the script inside it:
<?php
$memcache = new Memcache;
$memcache->connect开发者_C百科('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);
?>
In my route folder, this is how it looks like: http://i.imgur.com/kS1JY.png
In cPanel, the cron job runs the command:
/usr/bin/wget http://domain.com/x/activesessions_update.cron.php
I have no idea what the problem is. I am forced to delete 10,000s of these empty files every week. Please note that I have no experience in PHP programming as I did not code it myself so any replies would be appreciated with utter detail. Who can guide me to solve this puzzle?
EDIT: Got the solution from techincal support of my host:
It wasn't logging exactly, by default wget is used to download files. So when you run wget against that url it goes out, requests the file, and downloads the output of the request, essentially saving a copy of what you would get in your browser if you pulled it up. By adding the -O /dev/null to the command you are telling it that instead of saving that output to the default location (generally wherever it was being called from) to save it to /dev/null which is really just nowhere (basically just throws it away)
Since we have established that these are log files created by cron, a work-around is to have the PHP script delete the log files as it is run. Change activesessions_update.cron.php
to this - back up the original version in a different directory first!
I am assuming that the files are created in such a way that the user you script is run as has permissions to delete the files. If it is not, this wont work.
<?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);
?>
I guess the cron daemon on your server is configured to redirect STDOUT and STDERR of all the cron jobs it runs to a file. It seems odd that it is configured like this, as it will cause problems like you are having. Also, it seems very odd that it should redirect them to files that have, essentially, the same name of your script with a number on the end. You would have though they would be *.log
or something.
This solution will still leave at least one log file in existence at any one time, because you definitely wont be able to delete the file that is currently being written to.
If this does work, you can safely copy/paste the same code (from below the =====
comment line) into any other files that are called by cron jobs and are causing the same problem, as long as they are:
- creating the log files in the same directory that the script resides
- the only file in the directory (that you actually want) where the file name starts with the full name of the script file
I would bet that these are log files containing the output of the script, created by cPanel's cron. Check you cPanel's configuration and disable logging if you don't need it.
精彩评论