fun with hosting
November 27th, 2005 by herichon
Should it surprise me at all that the instructions given on my webhost’s site for running PHP scripts via cron are wrong and bad?
No, I don’t suppose it should.
FYI, if anyone out there using CPanel wants to run PHP off the cron tab, note that the instructions often recommended – ie,
get http://domain.com/script.php > /dev/null
may end up not only giving you emails after each execution of the script, but also dropping copies of script.php in your site’s home folder. Note also that hundreds or thousands (or, if you don’t check it for two months, tens of thousands) of files sitting in your site’s home folder will chew up disk space and are a pain in the ass to get rid of through FTP, if you host is not kind enough to provide shell access. (Mine, as you might guess, is not.) Add to that the hassle of deleting several thousand helpful notifications from Cron sitting in the admin mailbox, and you’ve got a nice mess to clean up.
Anyway, I’m no expert on this stuff, but here’s what worked for me:
The easiest way to get rid of the thousands of files sitting around was a simple
rm script.php.<strong>
(since they were stacking as script.php, script.php.1, script.php.2, etc)
Not having shell access, though, I couldn’t run it directly. So I just stuck the command on the crontab and waited for it to run. Sort of like shell access with lag, assuming your host lets you run commands like this via cron, and most will, since cron’s often used for clearing out log files and such. (Just remember to take it off the cron tab once you’re done, no point in it running repeatedly. And as always BE CAREFUL about what you put after the rm – for example, gods help you if you put a space between your script name and the asterisk.)
However, I had to modify the command a bit, since just rm script.php.</strong> was throwing an error about too many arguments – apparently there’s a kernel-level limit on how many arguments can be passed to rm. Luckily I found some info on this limitation here, along with the recommendation to try this command, to pipe the necessary arguments to rm individually through find:
find . -name 'script.php.*' | xargs rm
So I threw this on the crontab, let it run, and presto, no more 9000+ files.
As to the better way to run a PHP script off the cron tab on cPanel, a little checking around on sites like this one for something called Moodle revealed a better alternative:
w get -q -O /dev/null http://domain.com/script.php
(remove the space between the w and the get – for some reason WP won’t let me post this without the space, gr.)
Once the crap from the old cron jobs was cleared out, and this line was added, everything was fine – no copies of script.php stacking up on the server, and no emails hitting the admin account every time the cron job ran. Much better.