简体   繁体   English

shell_exec(); 在PHP中无法正常工作

[英]shell_exec(); not working properly in PHP

I'm trying to execute a command thru shell_exec(); 我正在尝试通过shell_exec()执行命令; but it does nothing. 但它什么也没做。 I know shell_exec(); 我知道了shell_exec(); is enabled because these work perfectly: 已启用,因为它们可以完美地工作:

echo shell_exec('which php');
echo shell_exec('ls');

They do output correctly. 他们确实输出正确。

The problem is when I want to execute something like this: 问题是当我想执行以下操作时:

shell_exec("php file.php var1 var2 > /dev/null &");

It does nothing, but when I run it thru the Terminal on my computer, it does work. 它什么也没做,但是当我通过计算机上的终端运行它时,它确实可以工作。 I have also tried doing it with the full path like this: 我也尝试过这样的完整路径:

shell_exec("/usr/bin/php /full/path/file.php var1 var2 > /dev/null &");

And it doesn't work, but again... it works if I run it thru my Terminal's computer. 而且它不起作用,但是再次...如果我通过终端计算机运行它,它将起作用。

The php.ini safe mode is off, and there are no disabled functions on it, so I don't know what could be happening. php.ini安全模式已关闭,并且上面没有禁用的功能,所以我不知道会发生什么。 And yes, I have access to SSH and got a dedicated IP. 是的,我可以使用SSH并获得专用IP。

Try to look at return code $return_var of the function (last arguments of functions system exec ). 尝试查看函数的返回代码$return_var (函数system exec最后一个参数)。 It should be 0 if success. 如果成功,则应为0 If it is 127 possibly command line is not permitted on the server or you don't have enough privilege to run commands from PHP scripts (it depends on the user/group from which scripts are executed). 如果是127可能是服务器上不允许使用命令行,或者您没有足够的特权来运行来自PHP脚本的命令(这取决于执行脚本的用户/组)。 Otherwise function simply returns empty array of output, like nothing good or bad happened. 否则,函数只会返回空的输出数组,就像没有发生好坏一样。

I ran into similar situation not so long ago, ls was working Ok, while wget was always failing. 不久前,我遇到了类似情况, ls工作正常,而wget总是失败。

You are probably get a permission denied error especially since you are trying to redirect the output to /dev/null . 您可能会收到一个权限被拒绝的错误,特别是因为您试图将输出重定向到/dev/null Might want to consider a cron job but I'm not sure why you are redirecting the output and running it in the background. 可能想考虑一项cron作业,但是我不确定为什么要重定向输出并在后台运行它。

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

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