Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从passthru和top获取某些信息_Php_Linux_Passthru_System Information - Fatal编程技术网

Php 从passthru和top获取某些信息

Php 从passthru和top获取某些信息,php,linux,passthru,system-information,Php,Linux,Passthru,System Information,我尝试过使用这种可能的解决方案,但没有任何运气: $test = passthru('/usr/bin/top -b -n 1'); preg_match('([0-9]+ total)', $test, $matches); var_dump($matches); 该代码显示以下文本: top - 19:15:43 up 31 days, 23 min, 1 user, load average: 0.00, 0.01, 0.05 Tasks: 85 total, 1 running, 84

我尝试过使用这种可能的解决方案,但没有任何运气:

$test = passthru('/usr/bin/top -b -n 1');
preg_match('([0-9]+ total)', $test, $matches);
var_dump($matches);
该代码显示以下文本:

top - 19:15:43 up 31 days, 23 min, 1 user, load average: 0.00, 0.01, 0.05 Tasks: 85 total, 1 running, 84 sleeping, 0 stopped, 0 zombie Cpu(s): 0.1%us, 0.0%sy, 0.0%ni, 99.8%id, 0.1%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 247980k total, 224320k used, 23660k free, 68428k buffers Swap: 521212k total, 37120k used, 484092k free [...] 0.4 0:00.00 top array(0) { }
如何获取任务总数等特定信息,使其仅显示例如
84 total


提前谢谢。

多亏了巴尔马和萨蒂什,我才让它工作起来。以下是正确的代码:

exec(“/usr/bin/top-b-n1 | grep任务:| awk-F”“{print$4}”)


非常感谢!:)

passthru()
不会返回输出,而是将其发送到浏览器。如果您想在变量中捕获输出,请使用
exec()
。不知道您是否尝试了以下方法:/usr/bin/top-b-n1 | grep Tasks:| awk-F''{print$2'$3}'Barmat:谢谢您的回答,但是
exec()
没有给我任何输出。萨蒂什:没有,但我现在测试它,它给我错误500(内部服务器错误)
passthru(“/usr/bin/top-b-n1 | grep任务:| awk-F”“{print$2”“$3}”)
。如果使用该命令使用
exec()
而不是
passthru()
,则会出现相同的错误。您需要转义嵌入的双引号,以便它们不会结束PHP字符串。
exec()
仅返回输出的最后一行。如果要查看其他行,必须传递可选的第二个参数。