我正在从PHP调用bash脚本,但我遇到了一个奇怪的问题,只有传递给bash的特定参数值才能成功执行:
我的PHP代码很简单:
$result = shell_exec("/scripts/createUser.sh $uname");Bash脚本代码:
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
usrname=$1
echo -e "Username: $1"
deploy="/wordpress.tar.gz"
dest="/data/$usrname"
mkdir -p $dest
cd $dest
tar zxvf $deploy -C $dest >/dev/null 2>&1
ls $dest但是,此脚本只能成功地进行mkdir、解压缩wordpress.tar.gz并在$uname == 'test'时列出文件夹,否则什么都不会发生(mkdir甚至不能)。
我将脚本转换为www用户,并授予执行权限,没有帮助。并且已经尝试过通过控制台作为root运行这些命令,它们运行得很好:
/scripts/createUser.sh admin
php deploy.php // in this script $uname == 'admin'怎么会发生这种事?谢谢你的主意!
发布于 2016-05-20 11:18:44
我最近发布了一个项目,允许PHP获得真正的Bash并与之交互(如果请求作为根),它解决了exec()和shell_exec()的限制。到这里来:https://github.com/merlinthemagic/MTS
下载后,只需使用以下代码:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', false);
$return1 = $shell->exeCmd("/scripts/createUser.sh " . $uname);
//the return will be a string containing the return of the command
echo $return1;但是,由于您只是简单地触发了一堆bash命令,所以只需使用这个项目一个一个地发出它们。这样,您还可以处理异常并准确地查看哪个命令返回一些意想不到的内容,即权限问题。
对于运行了很长时间的命令,您必须更改超时:
//is one hour enough?
$timeout = 3600000;
$return1 = $shell->exeCmd("/scripts/createUser.sh " . $uname, null, $timeout);https://stackoverflow.com/questions/31445659
复制相似问题