从了shell_exec命令,命令运行得到的输出 [英] get output from shell_exec command as command runs

查看:214
本文介绍了从了shell_exec命令,命令运行得到的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编码一个PHP脚本的网页,旨在接受一个JFFS2映像这是previously上传到服务器上的文件名。脚本是然后重新闪一个分区的服务器与图像,并输出结果上。我一直在使用这样的:

I am coding a PHP-scripted web page that is intended to accept the filename of a JFFS2 image which was previously uploaded to the server. The script is to then re-flash a partition on the server with the image, and output the results. I had been using this:

$tmp = shell_exec("update_flash -v " . $filename . " 4 2>&1");
echo '<h3>' . $tmp . '</h3>';

echo verifyResults($tmp);

(该verifyResults功能的更新成功完成回一些HTML,表示给用户的更新命令是否成功完成。也就是说,在该情况下,显示一个按钮,重新启动设备,等等。)

(The verifyResults function will return some HTML that indicates to the user whether the update command completed successfully. I.e., in the case that the update completes successfully, display a button to restart the device, etc.)

这里的问题是,update命令需要几分钟的时间才能完成,而PHP脚本块,直到shell命令完成后再返回任何输出。这通常意味着update命令将继续运行,而用户将看到一个HTTP 504错误(在最坏的情况),或者等待页面加载几分钟。

The problem with this is that the update command takes several minutes to complete, and the PHP script blocks until the shell command is complete before it returns any of the output. This typically means that the update command will continue running, while the user will see an HTTP 504 error (at worst) or wait for the page to load for several minutes.

我觉得自己应该做这样的事情,而不是:

I was thinking about doing something like this instead:

shell_exec("rm /tmp/output.txt");
shell_exec("update_flash -v " . $filename . " 4 2>&1 >> /tmp/output.txt &");
echo '<div id="output"></div>';
echo '<div id="results"></div>';

这在理论上把命令在后台添加的所有输出/tmp/output.txt。

This would theoretically put the command in the background and append all output to /tmp/output.txt.

然后,在JavaScript函数,我会定期要求getOutput.php,这将简单地打印/tmp/output.txt的内容并把它贴到输出分区。一旦命令被完全完成,另一个JavaScript函数将处理输出,并在结果格显示的结果。

And then, in a Javascript function, I would periodically request getOutput.php, which would simply print the contents of /tmp/output.txt and stick it into the "output" div. Once the command is completely done, another Javascript function would process the output and display a result in the "results" div.

不过,我在这里看到的问题是,getOutput.php最终将成为更新设备的闪存过程中不可访问,因为它是对分区的对象和更新。因此,有可能使我在同样的位置和以前一样,虽然没有504或看似永远加载页面。

But the problem I see here is that getOutput.php will eventually become inaccessible during the process of updating the device's flash memory, because it's on the partition to which is targeted for an update. So that could leave me in the same position as before, albeit without the 504 or a seemingly eternally-loading page.

我可以移动getOutput.php到另一个分区中的设备,但我想我还是会不得不做一些时髦的东西与Web服务器的配置才能够访问它那里(一个符号链接到它的根目录会,像任何其他文件,最终会再闪光期间覆盖)。

I could move getOutput.php to another partition in the device, but then I think I would still have to do some funky stuff with the webserver configuration to be able to access it there (a symlink to it from the webroot would, like any other file, eventually be overwritten during the re-flash).

有没有显示该命令的输出的任何其它方式运行,或者我应该只是凑合着解决方案,我有吗?

Is there any other way of displaying the output of the command as it runs, or should I just make do with the solution I have?

修改1:我目前正在测试的一些解决方案。我会更新我的问题有结果后。

Edit 1: I'm currently testing some solutions. I'll update my question with results later.

编辑2:看来这个文件系统没有被覆盖,因为我原本以为。相反,系统似乎安装在只读模式对现有的文件系统,所以我仍然可以访问getOutput.php即使在文件系统重新闪现。

Edit 2: It seems that the filesystem does not get overwritten as I had originally thought. Instead, the system seems to mount the existing filesystem in read-only mode, so I can still access getOutput.php even after the filesystem is re-flashed.

我在我的问题描述似乎除了使用代替了shell_exec的popen(如上文下面的答案)工作的第二个解决方案。该页面加载,并通过Ajax我可以显示output.txt的内容。

The second solution I described in my question does seem to work in addition with using popen (as mentioned in an answer below) instead of shell_exec. The page loads, and via Ajax I can display the contents of output.txt.

然而,似乎output.txt的不反映实际时间 - 这似乎显示什么,直到从执行更新命令返回重新刷新命令的输出。我需要做进一步的测试,看看是怎么回事。

However, it seems that output.txt does not reflect the output from the re-flash command in real time-- it seems to display nothing until the update command returns from execution. I will need to do further testing to see what's going on here.

修改3:没关系,它看起来像文件中的的电流,我访问它。我只是打一个延迟,而内核没有被我用在其上源JFFS2图像存储的分区引发一些JFFS2相关的任务。我不知道为什么,但是这显然会导致所有的PHP脚本阻止直到把它完成。

Edit 3: Never mind, it looks like the file is current as I access it. I was just hitting a delay while the kernel did some JFFS2-related tasks triggered by my use of the partition on which the source JFFS2 image is stored. I don't know why, but this apparently causes all PHP scripts to block until it's done.

要解决的是,我打算把更新命令调用一个单独的脚本,并通过Ajax--这种方式提出要求,用户至少会获得一定的prepackaged反馈,同时还专门守候在系统中。

To work around that, I'm going to put the update command invocation in a separate script and request it via Ajax-- that way, the user will at least receive some prepackaged feedback while technically still waiting on the system.

推荐答案

看的popen:的 http://it.php.net/manual/en/function.popen.php

这篇关于从了shell_exec命令,命令运行得到的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆