PHP Script Cron Problem


#1

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?


#2

If it works in the browser, then I suggest you use a wget cronjob:
/usr/bin/wget http://example.com/path/update.php

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


#3

This should work also, and it is more secure:

create a script (update.sh or something) with the following

[code]#!/bin/sh

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 update.sh

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

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>


#4

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


#5

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.


#6

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?


#7

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 update.sh 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 update.sh is in the same directory as the update.php.


#8

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.


#9

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

#!/bin/sh

cd home/kkotowick/forum.evetech-enterprises.com

/usr/local/bin/php update.php

And I’m getting an error saying:

“/home/kkotowick/update.sh: line 3: cd: home/kkotowick/forum.evetech-enterprises.com: 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.


#10

/home/kkotowick/forum.evetech-enterprises.com needs a leading slash.


#11

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