0

I'm trying to run a sh script file from the php function shell_exec() but every time the return is uncorrect.

This is the sh file content:

if [[ -z $1 ]]
then
        echo "svn st: NULL"
else
        command=$(ssh -q USER@IP_OF_THE_REMOTE_NODE "svn st $1")
        if [[ -z $command ]]
        then
                test="svn st: OK"
        else
                test="svn st: KO"
        fi
        echo $test
fi

every time that I run this sh script from the shell the echo is ALWAYS correct. The "$1" contains the working directory that I have to check.

But if I run the same script in this way:

$w_path = $working_path['path'];
$com = "sh /tmp/my_script_test.sh $w_path";

echo "content com ".$com;

$res = shell_exec($com);

echo "content res ".$res;

the last echo print ALWAYS "svn st: OK", that is correct for some $w_path, but completely uncorrect for others! Why? How can I find the mistake?

I don't think that is a ssh key problem cause I used a guide to resolve that and now I don't need to insert the password for the remote server.

Thanks Regards

4
  • Is you php script executed by the same user that executes the script? Enter whoami at the top of you sh file, your php script is probably run as www-data or apache (who does not have access to the remote server using a keypair). Commented Mar 12, 2013 at 16:28
  • Hi John thanks for your help. Yes in the second way the user is "Apache" and not "root", but I put the ssh key in var/www/.shh of the local server in the remote /root/.shh/authorized_keys. Do I have to do other things? Commented Mar 12, 2013 at 16:32
  • Change the command to sh -x /tmp/my_script_test.sh $w_path, so you'll see all the variable expansions. Commented Mar 12, 2013 at 16:33
  • Hi Barmar thanks for your help. I did as you said in your comment. Via shell I can see all the steps of the sh, via php I cannot see the same things, the shell exec return only the "whoami" and the usual "svn st: OK". It is frustrating! :S Commented Mar 12, 2013 at 16:40

1 Answer 1

2

Change

        command=$(ssh -q USER@IP_OF_THE_REMOTE_NODE "svn st $1")
        if [[ -z $command ]]

to

        if ssh -q USER@IP_OF_THE_REMOTE_NODE "svn st $1"

and remember: Whenever something doesn't work, it's because it's too complicated (can't recall where I read that).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.