Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t在PHP中运行bash脚本_Php_Linux_Bash_Swi Prolog - Fatal编程技术网

Can';t在PHP中运行bash脚本

Can';t在PHP中运行bash脚本,php,linux,bash,swi-prolog,Php,Linux,Bash,Swi Prolog,我正在尝试用PHP运行bash脚本,但无法运行它。 php-v Ubuntu 12.04 LTS 64位 我的php代码: $cmd='/www/var/pl/bash.sh'; $retval =-1; exec( $cmd, $output ); //executing the exec function. foreach( $output as $tmp ) { echo "$tmp <br>"; }; 我

我正在尝试用PHP运行bash脚本,但无法运行它。 php-v

Ubuntu 12.04 LTS 64位

我的php代码:

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


我可以在Linux终端上运行bash.sh。

很可能是路径或权限问题;例如,运行web应用程序的用户不知道
swipl
程序在哪里


执行它之前,将2>&1添加到命令行,以便它告诉您问题所在。或者,您可以在web服务器错误日志(或PHP错误日志;在PHP.ini中检查其路径和设置)中找到stderr输出。

当您在终端中运行脚本时,您将在登录的帐户下执行脚本。您有一个带有搜索路径等的shell设置

当php执行脚本时,它没有shell设置,而是在webserver用户帐户下运行。执行时:

  • 确保您有完整的文件路径,
    swipl
    是不够的,应该是
    /path/to/swipl
  • 确保Web服务器进程有足够的访问权限来获取所需的一切

尝试在shell脚本前面加上“bash”-这样“bash/www/var/pl/bash.sh”就可以将
$cmd
更改为
/www/var/pl/bash.sh 2>&1
。它仍然不会运行,但可能会让您了解为什么它不会运行。如果有错误,您会看到哪些错误?我假设您的PHP没有在安全模式下运行,并且exec命令没有被特别禁用?
    $cmd='/www/var/pl/bash.sh';
    $retval =-1;
    exec( $cmd, $output ); //executing the exec function.
    foreach( $output as $tmp ) 
    { 
        echo "$tmp <br>"; 
    };
 #!/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.