Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
1  
+1 - Fix the deepest problem you can find first. Then see if all the others went away. (Adapted from Axioms of Programming: "Fix the first error the compiler reported and rebuild. Chances are the rest will go away too.") –  voretaq7 Apr 9 '12 at 4:32
add comment

There's probably no reason to run it through a webserver/client request and doing so adds unnecessary complexity. Unless the PHP script requires the webserver environment ( such as $_SERVER or $_REQUEST ) then you're better off running it directly.

To run a PHP script directly from cron:

1 * * * * /usr/bin/php /path/to/your/php/script.php
share|improve this answer
    
i've done that but getting the error /bin/sh: /usr/bin/php: No such file or directory –  user112570 Apr 9 '12 at 0:03
    
@user112570 then locate where your php binary is. Either use "whereis php" or do a "find / -type f -name php" –  pauska Apr 9 '12 at 0:13
    
You may have PHP installed on a different path to what I wrote in the answer. You can use either which php or locate php to find it. –  Ladadadada Apr 9 '12 at 0:14
1  
Or you may have to install a separate package to get a command line PHP interpreter -- it's called php5-cgi on Debian and derived distros, and php-cli on RHEL and derived distros. But you definitely shouldn't be running scheduled tasks by hitting the webserver -- that way lies madness and timeouts and incomplete execution. –  womble Apr 9 '12 at 0:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.