php-如何在执行命令时从linux终端获取shell_exec()输出? [重复]

时间:2022-04-30 10:26:39

This question already has an answer here:

这个问题在这里已有答案:

i am running one linux command with shell_exec() in my php script.

我在我的PHP脚本中运行一个带有shell_exec()的linux命令。

$output = shell_exec('/usr/bin/linux-command ');

After execution of the command i am getting the value into a variable. That's okay.
But, here my linux command will give the output continuously in terminal.
It will come like this..

执行命令后,我将值变为变量。没关系。但是,这里我的linux命令将在终端中持续提供输出。会是这样的......

$linux-command  
123    456    789  
789    456    123  
123    456    789 .. //and so on..(by pressing 'Ctrl+c' only we can stop this. 

For every 5 seconds it will display one line. how to retrieve the line for every 5 sec.
My Page is in loading ........ Because, it is waiting for the $output.
Is there any way to solve my problem ??
Any idea is appreciated. Thank you in advance

每5秒钟它会显示一行。如何每5秒检索一次。我的页面正在加载........因为,它正在等待$输出。有什么方法可以解决我的问题吗?任何想法都表示赞赏。先谢谢你

2 个解决方案

#1


1  

Try the passthru function. The output is displayed directly. You can test it with this small script:

尝试passthru功能。输出直接显示。你可以用这个小脚本来测试它:

<?php
passthru('tail -f /var/log/apache2/access.log');
?>

#2


0  

You can use popen, check the manual for more details popen manual

您可以使用popen,查看手册以获取更多详细信息popen手册

#1


1  

Try the passthru function. The output is displayed directly. You can test it with this small script:

尝试passthru功能。输出直接显示。你可以用这个小脚本来测试它:

<?php
passthru('tail -f /var/log/apache2/access.log');
?>

#2


0  

You can use popen, check the manual for more details popen manual

您可以使用popen,查看手册以获取更多详细信息popen手册