简体   繁体   English

PHP exec bash脚本

[英]PHP exec bash script

I've written a bash script to take a screenshot and I want to launch it from a PHP page: 我已经编写了一个bash脚本来截取屏幕截图,我想从PHP页面启动它:

#!/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: 当我尝试从CLI启动它时,它按预期工作,但当我尝试使用PHP启动它时,它不起作用:

<?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). 正如你所建议我把脚本放入PATH eviroment变量(我编辑过.bash_profile)。 Now I can launch the command directly from CLI but the problem launching it from PHP stays the same. 现在,我可以直接从CLI启动命令,但是从PHP启动命令的问题仍然存在。

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: 编辑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); 它返回int(133); don't know what it means. 不知道这意味着什么。 Ps I've also edited the shebang. 附言:我也编辑过shebang。

EDIT 3: 编辑3:

var_dump of $array returns this: $ array的var_dump返回:

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"
}

The problem was generated because OSX and MacPorts are incompatible with each other for those libraries! 产生此问题的原因是OSX和MacPorts对于这些库彼此不兼容!

To solve this you have to edit as root the file /usr/pkg/sbin/envvars: 要解决此问题,您必须以root身份编辑文件/ usr / pkg / sbin / envvars:

Just: 只是:

  1. Comment out these lines 注释掉这些行

    DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH" DYLD_LIBRARY_PATH = “/应用程序/ MAMP /库/ lib目录下:$ DYLD_LIBRARY_PATH”

    export DYLD_LIBRARY_PATH 导出DYLD_LIBRARY_PATH

  2. Add this line at the end of the document 在文档末尾添加此行

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

确保bash可执行文件所在的目录位于PATH环境变量中... Web服务器环境已将此过滤掉的好几率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM