通过 HTTP 流式传输控制台输出(使用 Ruby)
我正在尝试远程运行一些命令,但无法通过 SSH 连接到计算机。我想做的是设置一个 Sinatra 应用程序来运行一些特定的命令并通过 HTTP 传输输出。
示例操作如下所示:
get "/log" do
`tail -f some.log`
end
1 据我所知,我需要使用 Unicorn (或 Mongrel),因为 Thin 不支持流数据 2 我认为我需要通过某种 IO ruby 对象通过管道输出命令,
我几乎知道如何执行 (1),但不知道如何实现 (2)。
I am trying to run some commands remotely and SSH'ing in to the machine is not an option. What I am trying to do is setup a Sinatra app that runs some specific commands and streams the output through HTTP.
The sample action looks like this:
get "/log" do
`tail -f some.log`
end
1 As far as I've read, I need to use Unicorn (or Mongrel) because Thin does not support streaming data
2 I think I need to pipe the commands output through some kind of IO ruby object
I almost know how to do (1) but have no idea how to achieve (2).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用的是同步服务器(即 Mongrel、Unicorn、not Thin),您可以只返回一个 IO 对象:
如果这不起作用(例如,如果您使用 Thin) ,您可以使用新的流 API:
您还可以使用 bcat gem,如果它包含 ANSI 颜色,它将对您的输出进行着色代码:
注意:对于无限运行的进程如果有人关闭连接,您必须自己负责终止该进程。使用第一个解决方案,某些服务器可能会为您解决这个问题。
If you're on a synchronous server (i.e. Mongrel, Unicorn, not Thin), you can just return an IO object:
If that doesn't work (if you're on Thin, for instance), you can use the new streaming API:
You can also use the bcat gem, which will colorize your output, if it contains ANSI color codes:
Note: For infinitely running process you'll have to take care of killing the process yourself if someone closes the connection. With the first solution some servers might take care of that for you.