Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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"
}
share|improve this question
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." – Linus Kleen Feb 14 '12 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! – Giorgio Feb 14 '12 at 10:49
I've edited the question :) – Giorgio Feb 14 '12 at 11:15
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.) – Linus Kleen Feb 14 '12 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 :( – Giorgio Feb 14 '12 at 11:54
show 2 more commentsadd comment (requires an account with 50 reputation)

2 Answers

up vote 1 down vote accepted

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. Commento 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"

share|improve this answer
add comment (requires an account with 50 reputation)

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.

share|improve this answer
I've edited the question including your answer, but unfortunately it doesn't work as expected. – Giorgio Feb 14 '12 at 11:12
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.