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.

  • 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

Go to http://www.php.net/manual/en/features.commandline.php 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

  • You are right! It works now. I'm very happy now. – Fer Dec 29 '09 at 3:02

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


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

  • 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. – Jeremy L Dec 29 '09 at 2:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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