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('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: https://i.stack.imgur.com/j3kp8.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.phpto 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.
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
*.logor 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: