1

I have a shell script named brew.sh as follows:

#!/bin/bash

brew doctor > test.txt

Which works fine when I run it from the command line. It outputs as expected the output of brew doctor to the test.txt file. However, when I call it from a php webpage as below it creates the test.txt file but the file is empty.

<?php

$output = shell_exec("./brew.sh");
echo $output;

?>

I suspected it was a permissions/ownership issue so I used chown to change ownership from username:staff to _www:_www on the parent directory,where it would then create the test.txt file but not write the output to it. I've even tried changing permissions to 777 temporarily on the test.txt and even that didn't work. Ultimately I would like to view the output of brew doctor and brew update on a webpage on my local development machine. Surely it shouldn't be that difficult?

Hoppo
  • 1,130
  • 1
  • 13
  • 32
  • does the `$PATH` for the user `_www` contain the path to `brew`, and has the user got the permissions required to run the bin? – Elias Van Ootegem Dec 13 '13 at 12:33
  • I've added brew to the apache path by following info here [environment variable for apache](http://stackoverflow.com/questions/6833939/path-environment-variable-for-apache2-on-mac) Still no joy. – Hoppo Dec 13 '13 at 12:49

2 Answers2

3

Elias is going to be right I suspect - your environment on the cmd line is not going to be like the environment the script finds itself in within the webserver, assuming the webserver user is even allowed to run a proper shell...

You'll need a path. Check what your path value is, and put it in.

Also doesn't hurt to always include the full path for your binary, i.e. brew.

#!/bin/bash
PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"
/usr/local/bin/brew doctor > /var/www/whatever/test.txt

.... personally.. This isn't the approach I would use. If this is for brew doctor and brew update I would just use cron jobs that redirect output to a file on the webserver since I wouldn't want the webserver invoking brew commands... someone could DoS the server pretty easily just by requesting those pages a lot. The cron solution is pretty bulletproof though - and in any case with cron I can run it as a more appropriate user where the permissions/environment for brew are likely to work more correctly in any case.

Also - the other guys point about checking errors is a good one. stderr is probably ending up in your web logs at the moment when you get empty output. 2>&1 can make it go into that file instead, probably.

[ Note that it has to be 2>&1 !! I think chris had a typo when he wrote >2&1 which does something very different. 2 is the stderr filehandle which we want to redirect to 1. >2 will redirect 1 (stdout) to a file called 2.

pacifist
  • 712
  • 4
  • 13
  • Good point on the webserver not being allowed to run a proper shell: it can't, the `.profile` isn't loaded, for example. The OP is going to have to use pipes, I think [as I did, after posting my first question on SO](http://stackoverflow.com/questions/9843550/load-profile-with-proc-open) – Elias Van Ootegem Dec 13 '13 at 12:58
  • Thanks pacifist, that explains why it was so difficult! Looks like I have a lot to learn. I've now set up a cron job which outputs to a basic.html page which does more or less what I was trying to do. – Hoppo Dec 13 '13 at 14:00
0

If you add 2>&1 to your shell command, you should get STDERR and STDOUT returned which will at least help you determine what is going on:

<?php

$output = shell_exec("./brew.sh >2&1");
echo $output;

?>
Digital Chris
  • 6,177
  • 1
  • 20
  • 29
  • I now get 2 files, one called test.txt which is blank, and the other called 2 which is also blank! – Hoppo Dec 13 '13 at 12:48
  • @DigitalChris: The script doesn't _have_ output, it consists of a single command, that redirects its standard output to a file. It's that file the OP wants to see filled – Elias Van Ootegem Dec 13 '13 at 12:56