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'm not really an administrator and need some help setting up a cron, well actually calling a PHP script from the command line in the first place.

I have a PHP5 script that I need to run at the following location:


Before setting up a cron for that, I wanted to test this manually, like so:

php /var/www/apps/myapp/services/myservice.php

This results in nothing. I can confirm that that script did not run. There is also no error. However, if I cd into the directory, and then call the script:

cd /var/www/apps/myapp/services
php myservice.php

It does work, as I can see from the log file of that script. It must be something incredibly simple that I'm overlooking, but how can I call that PHP script from the command line in one go, without going into the directory first?

PS: I'm on Debian Edge, using Apache2 and PHP5.

share|improve this question
Are you suppressing warnings using the '@' ? what is the script doing? Would it be possible for you to post a sanitized version of the script? – Zypher Dec 29 '09 at 1:45
Also don't forget php.ini defaults to not allowing short tags. In case you Google'd your way here wondering why your fresh php install didn't run your CLI scripts. – Chris K Sep 16 '14 at 0:58
up vote 2 down vote accepted

Go to and read the bulletpoint/info that says "The CLI SAPI does not change the current directory to the directory of the executed script!" This is probably why you're getting the error.

It should work if you make cron cd into your services directory before it runs the app like this:

01 * * * * myuser cd /var/www/apps/myapp/services; /usr/bin/php /var/www/apps/myapp/services/myservice.php

share|improve this answer
You are right! It works now. I'm very happy now. – Ferdy Dec 29 '09 at 3:02

Have you checked to make sure there's not an alias to php that's causing that behavior?

share|improve this answer

Do a which php from a command prompt to figure out the location of php and then modify your cron entry to call <path to php>/php /var/www/apps/myapp/services/myservice.php

share|improve this answer
That's completely unrelated. His PATH is obviously correct otherwise he'd be getting an error like "php: command not found" – rodjek Dec 29 '09 at 1:23
He might have two versions of PHP installed. – Nerdling Dec 29 '09 at 2:19

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.