简体   繁体   English

无法在PHP中运行bash脚本

[英]Can't run bash script in PHP

I'm trying to run bash script in PHP but can't run it. 我正在尝试在PHP中运行bash脚本,但无法运行它。 php -v 的PHP -V

PHP 5.3.10-1ubuntu3.2 with Suhosin-Patch (cli) (built: Jun 13 2012 17:19:58)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

Ubuntu 12.04 LTS 64 bit. Ubuntu 12.04 LTS 64位。

My php code: 我的PHP代码:

    $cmd='/www/var/pl/bash.sh';
    $retval =-1;
    exec( $cmd, $output ); //executing the exec function.
    foreach( $output as $tmp ) 
    { 
        echo "$tmp <br>"; 
    };

bash.sh: bash.sh:

 #!/bin/bash
swipl --quiet -s /var/www/pl/ples.pl -g "f(R, gel), writeln(R),open('/var/www/pl/in.txt',write, Stream),
write(Stream, (R)),
nl(Stream),
close(Stream)" -t halt.

What am I doing wrong? 我究竟做错了什么?

And I can run bash.sh in the Linux terminal. 我可以在Linux终端中运行bash.sh。

When you run the script in the terminal you are executing it under the account you are logged in to. 在终端中运行脚本时,您将以登录帐户执行该脚本。 You have a shell setup with a search path etc. 您有带有搜索路径等的shell设置。

When php executes the script, it has not a shell setup, and runs under the webserver user account. 当php执行脚本时,它没有外壳程序设置,并以Web服务器用户帐户运行。 When executing: 执行时:

  • make sure you have complete paths to your file, swipl is not enough, it should be /path/to/swipl 确保您有完整的文件路径, swipl是不够的,它应该是/path/to/swipl
  • make sure the webserver process has enough access rights to get everything it needs. 确保Web服务器进程具有足够的访问权限以获取所需的一切。

Most likely it is either a path or permission problem; 最有可能是路径问题或权限问题。 for example the user the web application runs as, has no idea where the swipl program is. 例如,Web应用程序的运行用户不知道swipl程序在哪里。

Add 2>&1 to the command line before exec 'ing it, so that it tells you what the problem is. exec之前将2>&1添加到命令行,以便告诉您问题所在。 Or you can find the stderr output into the web server error log (or PHP error log; check its path and settings in php.ini). 或者,您也可以在Web服务器错误日志(或PHP错误日志;在php.ini中检查其路径和设置)中找到stderr输出。

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

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