1

I've written a bash script to take a screenshot and I want to launch it from a PHP page:

#!/bin/bash

screenshot="screnshot.png"
screencapture='/usr/sbin/screencapture -xC'

if [ `whoami` == 'root' ]; then
loginpid=`ps -ax | grep [l]oginwindow.app | awk '{print $1}'`
  launchctl bsexec $loginpid $screencapture $screenshot
else
  $screencapture $screenshot
fi

When I try to launch it from CLI it works as expected but when I try to launch it with PHP it doesn't work:

<?php
exec("bash /Users/giorgio/Desktop/src.sh");     
?>

What's the problem with this?

EDIT:

As you've suggested I've put the script into the PATH eviroment variable (I've edited .bash_profile). Now I can launch the command directly from CLI but the problem launching it from PHP stays the same.

I've tried with those commands but neither of them seems to work:

exec("bash /usr/local/bin/screenshot.sh");
exec("bash screenshot.sh"); 
exec("screenshot.sh");  

EDIT 2:

I've tried to run the following code to indagate on what is happening to the called script:

<?php
$array = array();
$integer;
exec("bash /usr/local/bin/screenshot.sh 2>&1",$array,$integer);     
echo "<pre>";
var_dump($integer);
echo "</pre>";
?>

It returns int(133); don't know what it means. P.s. I've also edited the shebang.

EDIT 3:

var_dump of $array returns this:

array(5) {
  [0]=>
  string(51) "dyld: Symbol not found: __cg_jpeg_resync_to_restart"
  [1]=>
  string(134) "  Referenced from: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/ImageIO"
  [2]=>
  string(59) "  Expected in: /Applications/MAMP/Library/lib/libJPEG.dylib"
  [3]=>
  string(0) ""
  [4]=>
  string(95) "/usr/local/bin/screenshot.sh: line 11: 44948 Trace/BPT trap          $screencapture $screenshot"
}
5
  • 1
    From CGI context, "bash" might not be set in the environment. Alter the script's shebang to /bin/bash. Otherwise let us know, how it "doesn't work." Commented Feb 14, 2012 at 10:43
  • I've changed the sha-bang and it doesn't work either from the php script; I don't know how to be more specific on this because the script it's supposed to create a .png on the Desktop and when I launche the script from PHP it isn't created! Sorry, I'm a newbie on this! Commented Feb 14, 2012 at 10:49
  • 1
    What I meant is to alter the first line of the script to be executed (the one starting w/ #!). Editing .bash_profile is of no use either; it's not used in PHP-cgi. Please make full use of exec() diagnostics; examine the code in $return_var and the script's output. (Output redirection might be necessary; append 2>&1 to the command and you should see something in $output.) Commented Feb 14, 2012 at 11:25
  • Thanks for your efforts! I've done as you said and I get $output and $return_val now. However I don't know how to interpret them :( Commented Feb 14, 2012 at 11:54
  • Glad to have been of help (kinda). You can accept your own answer; this is possible here (in case you didn't know). Commented Feb 14, 2012 at 22:39

2 Answers 2

3

The problem was generated because OSX and MacPorts are incompatible with each other for those libraries!

To solve this you have to edit as root the file /usr/pkg/sbin/envvars:

Just:

  1. Comment out these lines

    DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"

    export DYLD_LIBRARY_PATH

  2. Add this line at the end of the document

    export PATH="$PATH:/opt/local/bin"

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

Comments

0

Make sure the directory where the bash executable is located is in the PATH environment variable... good odds that the web server environment has filtered this out.

1 Comment

I've edited the question including your answer, but unfortunately it doesn't work as expected.

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.