Cron – 403 forbidden error from cron

cron

I have some php code that runs fine in a browser but now I want to use the same code and execute it from a cron script I'm getting issues.

i tried the command on cron wget -O /dev/null http://www.mydomain.com/test.php
but if i try that in the terminal i get the error below.

What is the correct command to run a php file from cron? and do I need to add extra line of code to the top of my php file?

The problem I'm getting is

-bash-3.2$ wget -O /dev/null http://www.mydomain.com/test.php
--2012-04-08 15:59:41--  http://www.mydomain.com/test.php
Resolving www.mydomain.com... 46.***.***.1
Connecting to www.mydomain.com|46.***.***.1|:80... connected.
HTTP request sent, awaiting response... 403 Forbidden
2012-04-08 15:59:41 ERROR 403: Forbidden.

I gave the file 755 permissions and even 777 permissions, but can't see what I'm doing wrong.

Best Answer

Your webserver is not configured to allow access to the file you are trying to download. You need to solve that first before you attempt to run this download from cron.

First, figure out how your webserver is configured. If you are using apache you should be able to find /etc/apache/httpd.conf or /etc/apache2/httpd.conf or similar. That file should point you towards how your webserver is set up.

Second, check your error log. That should be something like /var/log/apache/error.log. That file should provide additional info on why the wget is failing.

Debug that problem. Once you get the wget working you can try running it in cron.

Also you could try using curl instead of wget. Something like this:

curl -s http://www.mydomain.com/test.php >/dev/null

should work fine too. I don't have a strong reason to prefer wget or curl in this case.

Related Topic