0

There is a website with a button, cliking the button should lunch a bash script that suppose to unmount a directory. The button calls functions.inc php script with this function:

function sftmz_release_s3test_connections($bucket_name){
    if($bucket_name == 's3test'){
        drupal_set_message('Check mount status ! - released?');
        $cmd = '/var/www/html/company/sites/default/modules/rp_minisite/admin/script.sh';
        exec($cmd);
    }
}

My problem is: When im in the shell and running the command:

/var/www/html/company/sites/default/modules/rp_minisite/admin/script.sh  

It works fine.

When I click the button, the test appears, but it does not run the script. How can i view logs ? Can i print logs to shell ? i cant since its being activated using a button on html...
I assume this is permission issues?

3 Answers 3

1

Try getting some more info about what actually happens when you call exec:

exec($cmd, $return, $status);
echo '<pre>';
var_dump($return);//is an array, containing the commands output
echo '</pre>';
if ($status === 0)
{//normally, if a cmd exits with 0, all is well
    echo 'Command executed';
}

If there is something funny going on, you might want to check if your script is relying on environment variables/aliases and the like being set. Perhaps you'll have to load a .profile file for the script to work.
Here you can see how to do this

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

16 Comments

when i echo, i dont see anything because the echo is being run from the php script and not by me using the shell, how can i, as a root, view the echo the script sends ?
the output of the script.sh script (STDOUT, STDERR) is redirected to the $return variable (second argument in the exec call), so the script's output can be found in $return, the echo is indeed the php script. Note: the script is being run as whichever user is running php, so you'll probably have to chmod the script so that all users can execute it
i've chmod 777 the script so i guess this is enough no ?
how can i view the output in the shell window or to write the return value to a file.. ? thanks
As I said before: exec($cmd, $output); file_put_contents('tmpFile.tmp', implode('\n', $output); will write the output to a tmpFile.tmp, but this goes through php. You can easily do this by adding this to your command: $cmd .= '> tmpFile.tmp': the > redirects the STDOUT stream to the file tmpFile.tmp. Job done...
|
0

Have you checked the return value of the exec() function ? See the doc for more info.

1 Comment

how can i view it ? if i do exec('script.sh',$output); and then echo implode("\n", $output) i cant view it, logged as root but cant see the echo from that php script since its being run from web
0

Try using the other arguments of the exec command..

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.