/usr/bin/env: php: No such file or directory

Solution 1:

Make sure that line endings and/or invisible spaces aren't causing the problem.

Remove the spaces in the first line of the script and insert new ones, making sure not to hold CTRL while pressing space.

Also, make sure you don't have DOS line endings (CR+LF). See https://stackoverflow.com/questions/82726/convert-dos-line-endings-to-linux-line-endings-in-vim for details.

Solution 2:

The env command will look through a user's $PATH to find the first executable of the given name. So, /usr/bin/env php will look for an executable file called php in any of the directories in the $PATH of the user running it.

In your case, that's almost certainly because when running a command over ssh, you don't start a full shell and don't actually read your shell's initialization files. You can check this by running this command (note the single quotes):

ssh [email protected] 'echo $PATH'

And comparing the output to what you get if you ssh [email protected] and then run echo $PATH. On my system. for example:

$ echo $PATH
/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:

$ ssh localhost 'echo $PATH'
/usr/bin:/bin:/usr/sbin:/sbin

Therefore, the $PATH that your script has access to when run with ssh [email protected] is not the same as when you log in to test it.

In any case, the simple solution is to use the full path to the interpreter instead of env. Both env and full paths have their benefits and drawbacks but, in this case, the path is safer:

#!/usr/bin/php

Solution 3:

Easiest way.... change the user's shell as the script.

/etc/passwd

Before:
deploy:x:0:0:,,,:/root:/bin/bash

After:
deploy:x:0:0:,,,:/root:/scripts/deploy.sh

Example script (make sure execute bit is set chmod +x)

/scripts/deploy.sh

#!/bin/bash 
PATH=$PATH:/moo etc...
moo.sh

Sample Works everytime! You should even be able to use the script to troubleshoot/debug any env variables which you may feel are unset etc ... also the handling arguments being passed into ssh will work as well..

Note: Its Best Practice to always FULLY QUALIFY the paths for any scripts, executables, etc.. Above is just an example which allows the default path to be set to call moo.sh within the moo folder ;)

That was easy.. Thanks for posting..

Reference: /etc/passwd format