I am trying to make a few php scripts that will run among other things system calls commands like top and other custom scripts. You can see the code below is super simple. The problem that we are having with the php below is that when I call the php from the linux prompt like:
我正在尝试制作一些PHP脚本,这些脚本将运行系统调用命令,如top和其他自定义脚本。你可以看到下面的代码非常简单。我们在下面的php中遇到的问题是当我从linux提示符调用php时:
#php checkTOP.php
it will return the top -n 1 output in the screen no problem there.
它将返回屏幕中的顶部-n 1输出没有问题。
When I run the script from the webserver using the http://url.com/checkTOP.php it only returns the following:
当我使用http://url.com/checkTOP.php从网络服务器运行脚本时,它只返回以下内容:
program:/usr/bin/top -n 1 ver1 = retval = 1 Returned not zero
程序:/ usr / bin / top -n 1 ver1 = retval = 1返回不为零
Which is my debugging statements.
这是我的调试声明。
<?php
$program="/usr/bin/top -n 1";
echo "program:{$program}<br /> \n";
$ver1=system($program,$retval);
echo "ver1 = {$ver1}<br />\n";
echo "retval = {$retval}<br /> \n";
if($retval==0)
{
echo "Returned 0<br />\n";
}
else
{
echo "Returned not zero <br />\n";
}
die;
?>
Change 1: One more thing. All permissions are set correctly /usr/bin/top is set root:apache with rxrxrx and also all the directories /usr/bin.
改变1:还有一件事。所有权限都设置正确/ usr / bin / top设置为root:apache with rxrxrx以及所有目录/ usr / bin。
6 个解决方案
#1
Ole beat me; I used output buffering so I'll post it:
奥莱打败了我;我使用输出缓冲所以我会发布它:
<?PHP
ob_start();
$program="/usr/bin/top -n 1 -b";
passthru($program);
$t=ob_get_contents();
ob_end_clean();echo "<PRE>$t</PRE>";
?>
$ t ”; ?>
#2
Sounds like it's a problem related to user accounts. When you run it from the command-line manually, you're probably running under a different user than the webserver tries to run as.
听起来这是与用户帐户相关的问题。当您从命令行手动运行它时,您可能在不同于Web服务器尝试运行的用户下运行。
Some things to check:
有些事要检查:
- is Safe Mode enabled for PHP?
- does the user the webserver runs under (often "www-data") have permission to execute
top
? - can you turn on a higher error reporting level to see if you can get more information?
是否为PHP启用了安全模式?
网络服务器运行的用户(通常是“www-data”)是否有权执行top?
你可以打开更高的错误报告级别,看看你是否可以获得更多信息?
#3
Depending on the information you want to gather from the system, it may be more useful, and certainly more secure to collect it from the /proc/ filesystem.
根据您要从系统收集的信息,从/ proc / filesystem收集信息可能更有用,也更安全。
What are you trying to get from the command?
你想从命令中获得什么?
#4
One way to get top output is to send -b switch to run top in batch mode and use exec() to get output in an array with one element per line.
获得最高输出的一种方法是在批处理模式下将-b开关发送到运行顶部,并使用exec()在数组中输出,每行一个元素。
<?php
exec('/usr/bin/top -n 1 -b',$output);
echo '<pre>',implode("\n",$output),'</pre>';
#5
It looks like system function returns fail and retval is probably null in that case.
看起来系统函数返回失败,在这种情况下retval可能为null。
If you can't get the system command you could try using exec and see if it works. It will also give you back everything you want and I think it also returns an error message to help debug the issue:
如果您无法获得系统命令,可以尝试使用exec并查看它是否有效。它还会为您提供所需的一切,我认为它还会返回错误消息以帮助调试问题:
#6
The top command does not normally send its output to stdout. You cannot capture it with the system command. Use the -b option to top for "batch mode" and it should work for you.
top命令通常不会将其输出发送到stdout。您无法使用system命令捕获它。使用-b选项顶部为“批处理模式”,它应该适合您。
$program="/usr/bin/top -b -n 1";
When you run it from the linux prompt, it looks like it's working because the output goes to the display. When I tried it, I noticed that the output did not quite match what I expected from the echo commands. That is what tipped me off that there was something strange about the output and a check of the man page for top confirmed the -b flag.
当你从linux提示符运行它时,看起来它正在工作,因为输出进入显示。当我尝试它时,我注意到输出与echo命令的预期不完全匹配。这就是让我感到惊讶的是输出有些奇怪,并且检查了top页面确认了-b标志。
--
bmb
#1
Ole beat me; I used output buffering so I'll post it:
奥莱打败了我;我使用输出缓冲所以我会发布它:
<?PHP
ob_start();
$program="/usr/bin/top -n 1 -b";
passthru($program);
$t=ob_get_contents();
ob_end_clean();echo "<PRE>$t</PRE>";
?>
$ t ”; ?>
#2
Sounds like it's a problem related to user accounts. When you run it from the command-line manually, you're probably running under a different user than the webserver tries to run as.
听起来这是与用户帐户相关的问题。当您从命令行手动运行它时,您可能在不同于Web服务器尝试运行的用户下运行。
Some things to check:
有些事要检查:
- is Safe Mode enabled for PHP?
- does the user the webserver runs under (often "www-data") have permission to execute
top
? - can you turn on a higher error reporting level to see if you can get more information?
是否为PHP启用了安全模式?
网络服务器运行的用户(通常是“www-data”)是否有权执行top?
你可以打开更高的错误报告级别,看看你是否可以获得更多信息?
#3
Depending on the information you want to gather from the system, it may be more useful, and certainly more secure to collect it from the /proc/ filesystem.
根据您要从系统收集的信息,从/ proc / filesystem收集信息可能更有用,也更安全。
What are you trying to get from the command?
你想从命令中获得什么?
#4
One way to get top output is to send -b switch to run top in batch mode and use exec() to get output in an array with one element per line.
获得最高输出的一种方法是在批处理模式下将-b开关发送到运行顶部,并使用exec()在数组中输出,每行一个元素。
<?php
exec('/usr/bin/top -n 1 -b',$output);
echo '<pre>',implode("\n",$output),'</pre>';
#5
It looks like system function returns fail and retval is probably null in that case.
看起来系统函数返回失败,在这种情况下retval可能为null。
If you can't get the system command you could try using exec and see if it works. It will also give you back everything you want and I think it also returns an error message to help debug the issue:
如果您无法获得系统命令,可以尝试使用exec并查看它是否有效。它还会为您提供所需的一切,我认为它还会返回错误消息以帮助调试问题:
#6
The top command does not normally send its output to stdout. You cannot capture it with the system command. Use the -b option to top for "batch mode" and it should work for you.
top命令通常不会将其输出发送到stdout。您无法使用system命令捕获它。使用-b选项顶部为“批处理模式”,它应该适合您。
$program="/usr/bin/top -b -n 1";
When you run it from the linux prompt, it looks like it's working because the output goes to the display. When I tried it, I noticed that the output did not quite match what I expected from the echo commands. That is what tipped me off that there was something strange about the output and a check of the man page for top confirmed the -b flag.
当你从linux提示符运行它时,看起来它正在工作,因为输出进入显示。当我尝试它时,我注意到输出与echo命令的预期不完全匹配。这就是让我感到惊讶的是输出有些奇怪,并且检查了top页面确认了-b标志。
--
bmb