我试图在 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/