Today I had a problem with a site I had a PHP script on, running as a cron job. Lo and behold, as I went through the directories via FTP, I found several hundred small files all looking somewhat like the PHP script path – here’s an example with details tweaked to protect the guilty:
The cron job to run it was this one:
*/15 * * * * wget http://egwebsite.com/myscript.php?p=1234
Which loads the myscript.php web page every 15 minutes, doing its business.
WGET is a handy tool to grab pages via URL rather than direct PHP calls – in this case, I needed to pass a parameter via $GET (?p=1234), and so a direct PHP call was awkward. I also wanted the option to call the file via my browser from time to time, so storing it out of the HTML document directories was not an option either.
However, I had cruft – and the cruft in my root directory was obviously related to this – in fact, each file seemed to be the page output, same as if I had it on my browser.
What to do?
- The obvious answer in hindsight would be to dig around for the WGET option to not cache or log, and so not create any file. However, I thought originally the problem was ALL cron jobs, not just ones using WGET. Nonetheless, if you want, here is an option I found online (although didn’t test it, so Caveat emptor) which sends all output to /dev/null, effectively trashing it:
wget -qO- http://egwebsite.com/myscript.php?p=1234 &> /dev/null
- Call PHP directly. While I was blaming cron generally, I tried a direct PHP call, like this:
php /home/egwebsitecom/myscript.php >/dev/null 2>/dev/null
On HostGator at least, this worked. One awkward thing: the command line parameters. To call that, I ended up creating two scripts – myscript0.php which called myscript.php.
In myscript0.php I simply did this:
and in myscript.php I did this:
// code to set the GET parameter(s) directly
// code to set parameter(s) from the URL GET variables
Now my file could be called directly in the browser, or via PHP in cron, bypassing WGET.
A few notes:
- While trying these out, I recommend you add an email address via cron in cPanel to get reports – this is the easiest way to confirm everything is working.
- As well, leave off the null output for now (the >/dev/null 2>/dev/null) so the result will be emailed to you, helping with troubleshooting.
- Finally, while testing, bump up the cron time to say every 1 or 2 minutes for a little while, so you get responses fast – but don’t forget to slow it down after you’re done – servers don’t like a once a minute cron job!