Linux echo exec 工作但 exec 不

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7839699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:46:45  来源:igfitidea点击:

echo exec working but exec not

phplinuxshellexecshell-exec

提问by Udit Gupta

I am using red hat enterprise edition n try making a simple php page..

我正在使用红帽企业版尝试制作一个简单的 php 页面..

When I try with ...

当我尝试...

 // html code
 <?php
 echo exec(<cmd>); 
 ?>
// rest html code

Its working fine

它的工作正常

but when tried with ...

但是当尝试...

 // html code     
 <?php
 exec(<cmd>);
 ?>
 // rest html code

Its not working

它不工作

even a simple command like cat,ls,etc not working and also I tried 2 > &1then no error is printed .

即使是像 cat、ls 等简单的命令也不起作用,我也尝试过,2 > &1然后没有打印错误。

What could be the possible error ???

可能的错误是什么???

采纳答案by Phill Pafford

Docs:

文档:

return a response from the command, you would need to print the response out as well

从命令返回响应,您还需要打印出响应

Example:

例子:

<?php
$response = array()
exec('whoami', $response);
print_r($response,true);
?>

回答by Udit Gupta

okkkkkkk ......... I solved the problem. Actually there were two issues ...

okkkkkkk .........我解决了这个问题。其实有两个问题...

  1. The apacheuser searches its command in /usr/binfolder by default and the command I was trying to use was located in /usr/local/bin. So I need to create a soft link of that command in the /usr/bindirectory.

  2. Secondly , apacheis a less privilaged user than root so need to on the sticky bitof command so that apache could successfully run the command.

  1. apache用户在搜索其命令/usr/bin的文件夹在默认情况下,我试图使用命令位于/usr/local/bin。所以我需要在/usr/bin目录中创建该命令的软链接。

  2. 其次,apache是比 root 权限更低的用户,因此需要使用sticky bitof 命令,以便 apache 可以成功运行该命令。

I hope this will help someone else also who will face the same problem in future.

我希望这能帮助将来也面临同样问题的其他人。