Jump to content

Weird path problem when calling a PHP script from the command line


Recommended Posts

 

 

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 Etch, using Apache2 and PHP5.

 

Command line script should start with a shebang line which tells your shell what interpreter to use to execute the script. In your case you want to use php so place this on the very first line of your script.

 

#!/usr/bin/php

 

That path should be correct for Debian.

 

Next thing is to make sute the script is executable.

 

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

 

There is no reason why you would need to be within the same directory as the script in order to execute it. How exactly are you confirming it didn't run?

I did put the shebang in it and I did make sure it is executable, sorry for not mentioning that earlier.

 

I did manage to get it to work now, but only by cd-ing into the dir first from the cron job. I'm happy it work, but it is still unexplained why it will not run from any dir, given that I use absolute paths everywhere.

Command line script should start with a shebang line which tells your shell what interpreter to use to execute the script.

 

That's not necessarily when you pass the file path to the interpreter and run that instead.

Command line script should start with a shebang line which tells your shell what interpreter to use to execute the script.

 

That's not necessarily when you pass the file path to the interpreter and run that instead.

 

Of course, I am aware of that.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.