Debian – PHP from command line…path problems

command-line-interfacedebianphp5

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:

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

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.

Best Answer

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