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?

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
ilansch
  • 4,784
  • 7
  • 47
  • 96

3 Answers3

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

Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • 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 ? – ilansch Dec 31 '12 at 12:21
  • 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 – Elias Van Ootegem Dec 31 '12 at 12:32
  • i've chmod 777 the script so i guess this is enough no ? – ilansch Dec 31 '12 at 12:36
  • how can i view the output in the shell window or to write the return value to a file.. ? thanks – ilansch Dec 31 '12 at 12:49
  • @ilansch: of course that should be enough (but don't chmod to 777 on your actual site: 777 is good for quick testing, but not safe for production environments). Could you post your shell script (perhaps there's a problemn there: you might have forgotten the hashbang `#!/bin/bash`...) – Elias Van Ootegem Dec 31 '12 at 12:49
  • you needn't see a shell window, the output should be sent to the `php://STDIN` stream so you can simply write `file_put_contents('output.tmp',implode("\n", $result));` but again: the bash script has to start with a hashbang, and the correct environment has to be loaded (if required). Check the link to that question for alternatives. [or make a pipe](http://stackoverflow.com/questions/10102952/proc-open-interaction) – Elias Van Ootegem Dec 31 '12 at 12:52
  • the bash scripts works well, if I do /var/www/html/company/sites/default/modules/rp_minisite/admin/script.sh -> it works ok, i have the #!/bin/bash on it. – ilansch Dec 31 '12 at 12:59
  • How can i view the chmod of the folder ? Maybe its the folder premission ? i want to set it temporary to 777 and then to return to current chmod.. thanks – ilansch Dec 31 '12 at 13:03
  • What does your current `exec` call look like? Are you passing 2 or three arguments? if so, and if still can't see a thing, try using `system`, it returns _all_ output (exec only returns the __last line of output__, which might be the cause of your issues) – Elias Van Ootegem Dec 31 '12 at 13:04
  • To see the access rights of a folder, and all files in it: `ls -lta --color` – Elias Van Ootegem Dec 31 '12 at 13:04
  • my current exec look like the one i pasted above since none of the changes took effect – ilansch Dec 31 '12 at 13:32
  • @ilansch: well, that's not going to work, ever: you're not capturing the output then. Try `echo system($cmd);` for a change... or have a look at the link in one of my comments to an example that uses pipes, that'll sort you out – Elias Van Ootegem Dec 31 '12 at 14:14
  • Hi, i assume that the druple does not have permission to run bash script ? – ilansch Dec 31 '12 at 14:41
  • It does, you chmodded it to 777, so everybody can run the script. if you're calling it using exec, the way you posted it _might_ run, it _might_ not, but you're not capturing the output, nor the status (third argument). Just _read the docs_, and give some more input. I don't know if you get a 0 status reply, I don't know if a warning was raised... I can't see what your doing at all. I don't know if you've tried using `system`, or `passthru` or `proc_open`... I can't help you if I don't know the whole story, now can I? Honestly: more info is needed! – Elias Van Ootegem Dec 31 '12 at 14:47
  • OK, so my current problem is to view the output of the exec. since the exec is running from php script, and not from the telnet, echo it wont print the output in the telnet window. so how can i print the exec results to a temporary file or something like that ? so i can view them – ilansch Dec 31 '12 at 14:58
  • 1
    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... – Elias Van Ootegem Jan 01 '13 at 05:18
0

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

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • 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 – ilansch Dec 31 '12 at 12:25
0

Try using the other arguments of the exec command..

bartlaarhoven
  • 825
  • 2
  • 8
  • 21