php - 无法在 PHP 中运行 bash 脚本

标签 php linux bash swi-prolog

我试图在 PHP 中运行 bash 脚本,但无法运行。 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 位。

我的 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:

 #!/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.

我做错了什么?

而且我可以在 Linux 终端中运行 bash.sh。

最佳答案

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

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

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

关于php - 无法在 PHP 中运行 bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12543064/

相关文章:

php - MySQL - 插入多个插入,页面停止工作

php - 使用PHP更改服务器的IP地址

c++ - 启动和监视另一个进程的 Linux 最佳实践

linux - 这些输出是什么意思?

linux - 获取执行 bash 脚本的负载、cpu 使用率和时间

bash: 'map' 函数参数?

bash - 如何有条件地运行 github 工作流作业并在作业之间传递环境变量

php - 防止共享下载 URL

php - 我怎样才能用这个 MySQL 查询做一些额外的事情?在 PHP 中

php - 清除 Laravel 的 orderBy