简体   繁体   English

无法从 php web 脚本运行 shell 脚本

[英]Can't run shell script from php web script

I am trying to run a shell script from a php script.我正在尝试从 php 脚本运行 shell 脚本。

I have complete control of the environment (unix on mac), I should have all the permissions, etc. set correctly.我可以完全控制环境(mac 上的 unix),我应该正确设置所有权限等。

The web script is in /htdocs/ web 脚本位于/htdocs/

The shell script can be executed from anywhere so when I go to /htdocs/ in the shell, I can easily run it like this: $ my_shellscript shell 脚本可以从任何地方执行,所以当我 go 到 shell 中的 /htdocs/ 时,我可以像这样轻松运行它:$ myshellscript

.. but when my php script (which is located in htdocs) tries to call it: .. 但是当我的 php 脚本(位于 htdocs 中)尝试调用它时:

shell_exec('my_shellscript');

I get nothing.我什么都得不到。

I have proven the script can be called from that location and I have temporarily granted full access to try to get it working somehow.我已经证明可以从该位置调用该脚本,并且我已暂时授予完全访问权限以尝试使其以某种方式工作。 I am going crazy, please help.我要疯了,请帮忙。

If you know of some other way of triggering a shell script via the web that would be fine.如果您知道通过 web 触发 shell 脚本的其他方式,那很好。

Thanks in advance.提前致谢。

well i got few weeks same problem, the solution is to check if the apace has the permission to execute your script.好吧,我遇到了几周同样的问题,解决方案是检查 apace 是否有权执行您的脚本。 You could also try to run the script in php cli.您也可以尝试在 php cli 中运行脚本。

Since it is a shellscript, it needs to be invoked with the path prefix.由于它是一个shellscript,因此需要使用路径前缀来调用它。 My guess is you need to do this:我的猜测是你需要这样做:

shell_exec('./my_shellscript');

First thing: make sure php isn't running in Safe Mode第一件事:确保 php 没有在安全模式下运行

Next thing: Try running it with the exec() function and using the full path (eg /var/www/htdocs/my_shellscript)接下来:尝试使用exec() function 运行它并使用完整路径(例如 /var/www/htdocs/my_shellscript)

Try doing尝试做

echo shell_exec('my_shellscript 2>&1');

which will capture the script's stderr output and print it out.这将捕获脚本的标准错误 output 并将其打印出来。 If something inside the script is failing, this output would otherwise be lost when not being run interactively.如果脚本中的某些内容失败,则此 output 将在不以交互方式运行时丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM