PHP Script Cron Problem

Ok, so I make this big fancy script for auto-updating my database (including my phpBB database to activate/deactivate users). I do all my testing and debugging just by visiting the page in my browser so it executes, and get it working perfectly.

So, I try to set up a cron job with my new script. Lo and behold, it doesn’t work. So I talk to the dreamhost tech guy and he says “duh, it runs differently when it’s a cron compared to visiting it in a browser”. I just don’t know what exactly he meant by that, or how to fix it.

When I go to the file (update.php) in a browser, it executes through just fine. However, when run through a cron job, the email containing the results of the job has an error:

The config.php file could not be found.

Click here to install phpBB

Any ideas why this is happening?

If it works in the browser, then I suggest you use a wget cronjob:

Or maybe someone will come along who’s run across this in phpBB.

This should work also, and it is more secure:

create a script ( or something) with the following


cd (absolute path to directory that contains update.php)

/usr/local/bin/php update.php[/code]

Edit: Used full path to php. I’m not sure why it has not been found in the path.

chmod +x

change your cron job to run sh /path/to/

For extra security, prevent the file form being run in a browser by adding this to a .htaccess file in the same directory as update.php:

<Files update.php> order deny,allow deny from all </Files>

Well that “wget” solution worked just perfectly. I also added the .htaccess file for good measure. Thanks!

And it still works with the .htaccess deny? Wget is essentially a command-line web browser, so it’s hitting update.php the same way Firefox, etc. would, which the .htaccess file is trying to prevent.

The browser solution works because it is being executed differently than cron would do it, so the relative paths work. That was probably what the support tech meant in that rather rude and unhelpful remark. Basically the original problem occurred because php was being executed by cron outside of the directory where config.php existed. The script I posted above solves that. You are right, wget should not work with that .htaccess file at all. I wonder if the file was added after wget was tested?

Ok no you’re right, the wget didn’t work with the .htaccess. So I guess that puts wget out of the question because I really would like to lock out the update.php from browser access.

So I tried the idea, and it has some problems too. When the cronjob emails me the result, it says:

“line 3: php: command not found”

I used exactly the script you posted, but without the “cd” because the is in the same directory as the update.php.

Keep the cd. That is what will make it work. Otherwise it will fail like it did before. Sorry, php should be /usr/local/bin/php.

Ok, so I put the cd back in, now the script is as follows:


cd home/kkotowick/

/usr/local/bin/php update.php

And I’m getting an error saying:

“/home/kkotowick/ line 3: cd: home/kkotowick/ No such file or directory”

and I’m pretty damned sure that directory exists, cause I run a website off of it. [/php][/quote][hr]
No thoughts? Yeah, it stumped me too.

/home/kkotowick/ needs a leading slash.

um, yep, thanks for making me feel like an idiot. It works.