bash脚本由PHP的exec()没有做重定向正常运行 [英] Bash script run by PHP exec() not doing redirection properly

查看:156
本文介绍了bash脚本由PHP的exec()没有做重定向正常运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,需要一些参数和重定向定界符的输出到另一个文件,以创建Web服务器的虚拟主机文件。
当我运行在命令行脚本,它执行罚款和创建文件,但是当我运行它使用PHP的EXEC或了shell_exec不创建该文件。其他一切(或相呼应试图简单地返回输出而不是重定向它)运行良好 - 只需创建该文件不发生,我收到没有迹象显示有问题

下面是我的code:
create_vhost.sh:

 #!/斌/庆典
#使用模板来创建虚拟主机文件
回声创建虚拟主机中的文件的网站,提供
须藤/absolute/path/to/vhost_template.sh $ 1 $ 2 - ;在/ etc / nginx的/网站的可用/ $ 2
nginx的服务启动

我运行上面的脚本,像这样: /absolute/path/to/create_vhost.sh SITE_ID domain.com 和它完美的作品与使用SSH终端

有关参考,vhost_template文件是这样的:

 #!/ bin / sh的SITE_ID = $ 1
DOMAIN = $ 16#定义的模板。
猫<< EOF
[模板code这里$ SITE_ID和$域参数某处]
EOF

但是,如果我运行它在PHP中像这样:

 回了shell_exec(/绝对/路径/要/ create_vhost.sh SITE_ID domain.com);

我得到的输出就好了,但不会创建文件!就像我说的,如果我把重定向,那么即使在PHP中我看到了模板文件的输出,没有问题。

我一直在寻找这几个小时,但我只是难倒。有什么建议?

编辑:只是想我会提到,我认为这可能是一个权限的事情,但我尝试使用了shell_exec()内的须藤,并使用visudo命令给www数据的用户sudo特权,还是一样的问题...


解决方案

您可能会错过发送到标准错误所以你可以尝试重新定向某些重要信息标准错误输出到标准输出。会帮你调试的问题。

 回了shell_exec(/绝对/路径/要/ create_vhost.sh SITE_ID domain.com 2  - ;&安培; 1);

I have a script that takes some parameters and redirects the output of a heredoc to another file, in order to create a virtual host file for the web server. When I run the script in the command line, it executes fine and the file is created, but when I run it using PHP's exec or shell_exec it doesn't create the file. Everything else (echoing or trying to simply return the output instead of redirecting it) runs fine - just creating the file doesn't happen and I receive no indication that there was a problem.

Here is my code: create_vhost.sh:

#!/bin/bash
#Use the template to create the vhost file
echo "Creating vhost file in sites-available"
sudo /absolute/path/to/vhost_template.sh $1 $2 > /etc/nginx/sites-available/$2
service nginx restart

I run the above script like so: /absolute/path/to/create_vhost.sh site_id domain.com and it works perfectly using ssh with Terminal.

For reference, the vhost_template file is like this:

#!/bin/sh

SITE_ID=$1
DOMAIN=$2

#define the template.
cat  << EOF
[template code here with $SITE_ID and $DOMAIN parameters somewhere]
EOF

However, if I run it in PHP like so:

return shell_exec("/absolute/path/to/create_vhost.sh site_id domain.com");

I get the output just fine, but no file is created! And like I said, if I drop the redirection, then even in PHP I see the output of the template file with no problem.

I've been looking at this for hours but I'm just stumped. Any suggestions?

EDIT: Just thought I'd mention that I thought it's probably a permissions thing, but I tried using sudo within shell_exec(), and using visudo to give the www-data user sudo privileges. Still the same problem...

解决方案

You could be missing out some vital information that is sent to stderr so you can try redirecting the stderr output to stdout. Will help you debug the issue.

return shell_exec("/absolute/path/to/create_vhost.sh site_id domain.com 2>&1");

这篇关于bash脚本由PHP的exec()没有做重定向正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆