I have this weird problem. The php command is not found in one specific sitiation.
I try to call the command in three different ways. Only one of them fails:

  1. Being user john I create a simple script which calls php --version. Then as user root I call that script like this: sudo -u john ./script.sh
    This way the php command is not found.

  2. Being root I invoke the command directly like this: sudo -u john php --version
    No problem here, the command does exist.

  3. I call the script directly as user 'john': ./script.sh
    It works fine too.

What is causing this?

The problem happens with other users too, not just john, and it happens on this particular machine only.
The OS is CentOS 5.9.

EDIT: Also, other commands like gcc, perl or python don't present this problem. The php command is the only one I found so far with the problem.

  • 1
    May I suggest comparing path variables on the accounts? /home/"username"/.bash_profile Simply add php executable location to the path. – Vick Vega Apr 7 '13 at 3:31
  1. Your test script lacks a shebang line. No soup for you.
  2. The subshell being invoked by your script is not using the same PATH variable as your interactive shell. Type echo $PATH to see your current path variable, and set this PATH variable within the script.

As for why the PATH variable isn't consistent between the two:

  • Because you aren't specifying the shebang line, you have no insurance that the shell being invoked is the same as your interactive shell. It could be sourcing different configuration files entirely.
  • Different configuration files get sourced depending on how a shell is executed. You are inside of an interactive shell, and your script is a non-interactive shell. Assuming you're trying to use bash, consult man bash and begin reading at the INVOCATION section near the top of the file.
  • Thanks. The difference in the $PATH variable was the problem. The php command wasn't in /usr/bin as it normaly is, so I just added a symlink to it and problem solved. – GetFree Apr 7 '13 at 4:02
  • @GetFree I really don't recommend that symlink, it will just make things confusing. The proper solution to the problem is to understand why the PATH variable was different, and correct it in the proper configuration file. – Andrew B Apr 7 '13 at 4:05
  • I think the difference in PATH is due to .bash_profile as @Vick said. The PATH inside the script was just /usr/bin:/bin while in the interactive shell it was a much longer list of paths. – GetFree Apr 7 '13 at 4:20
  • 1
    @GetFree I know about .bash_profile, I was directing you to the invocation docs earlier. Symlinks aren't the right way to solve this problem in a professional IT environment, I'll leave it at that. – Andrew B Apr 7 '13 at 4:24
  • 2
    Specifying the absolute path to the php binary in the script would be my preferred solution. This makes your script independent of any $PATH variable (although this could also be a problem when different users are intended to run different PHP binaries). – Lukas Apr 7 '13 at 5:28

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.