Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 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
--2012-04-08 15:59:41--
Resolving 46.***.***.1
Connecting to|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
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 >/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 - 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

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
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

Your Answer


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.