处理守护进程的输入和输出
我想在 Linux 上创建 ruby 程序的守护进程。
我还希望守护进程是交互式的 - 我希望能够通过文件/管道/最简单的方法将输入发送到守护进程,并将输出接收到文件。
我该怎么做呢?
我研究了模块守护进程(http://daemons.rubyforge.org/)、线程和方法 popen3,但我很难让它们执行上述操作。
回答: Mladen 的方法:
我有创建守护进程的控制器:(您需要守护进程模块 gem)
require 'rubygems'
require 'daemons'
Daemons.run('/myDaemon.rb', {:app_name => "o", :dir_mode => :normal, :dir => '', :log_output => true, :multiple => true })
这是 myDaemon.rb:
puts `pwd`
File.open('my_pipe', 'r+') do |f|
loop do
line = f.gets
puts "Got: #{line}"
end
end
步骤: 这两个文件都在我的根目录\. Daemons.run 在 \ 中创建守护进程。
创建命名管道 mkfifo ./my_pipe。
rubycontroller.rb启动
cat> my_pipe
输入文本
ctrl-c 停止输入
cat o.output 查看输出
I want to create a daemon of a ruby program on Linux.
I also want the daemon to be interactive-I want to be able to send input to the daemon through a file/pipe/the simplest method and receive output to a file.
How do I go about doing this?
I've looked into the module daemons (http://daemons.rubyforge.org/), threads and the method popen3 but I'm having a hard time getting them to do the above.
ANSWER:
Mladen's method:
I have the controller that creates the daemon: (you'll need the daemons module gem)
require 'rubygems'
require 'daemons'
Daemons.run('/myDaemon.rb', {:app_name => "o", :dir_mode => :normal, :dir => '', :log_output => true, :multiple => true })
Here's myDaemon.rb:
puts `pwd`
File.open('my_pipe', 'r+') do |f|
loop do
line = f.gets
puts "Got: #{line}"
end
end
Steps:
Both files are in my root directory \.
The Daemons.run creates the daemon in \.
Create a named pipe, mkfifo ./my_pipe.
ruby controller.rb start
cat > my_pipe
type text
ctrl-c to stop input
cat o.output to see your output
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能是最简单的方法,命名管道,基于 http://www.pauldix.net/2009/07/using-named-pipes-in-ruby-for-interprocess-communication.html:
步骤1:创建命名管道
第 2 步:创建“守护进程”:
并运行它。
步骤 3:打开另一个终端并运行
并开始逐行输入一些文本。
步骤 4:观察守护进程的输出。
第5步:???
第六步:利润。
Probably the simplest way, named pipes, based on http://www.pauldix.net/2009/07/using-named-pipes-in-ruby-for-interprocess-communication.html:
Step 1: Create a named pipe
Step 2: Create your "daemon":
and run it.
Step 3: Open another terminal and run
and start typing some text, line by line.
Step 4: Watch output of the daemon.
Step 5: ???
Step 6: Profit.
打开绑定到端口的 socket未使用,但您和想要与其通信的程序都知道它。如果守护进程只需要与同一台机器上的进程通信,则使用 Unix 域套接字(请参阅 Socket.unix_server_loop )。如果它还需要与运行它的主机外部的进程进行通信,那么您需要打开一个互联网套接字(请参阅
Socket.tcp_server_loop
)。服务器的一般配方是:
在客户端:
您的服务器和客户端需要就谁先发送什么以及另一方的适当响应达成一致。
Open a socket bound to a port which is unused, but which is known to you and the program(s) which want to communicate with it. If the daemon only needs to talk with processes on the same machine, then use a Unix domain socket (see
Socket.unix_server_loop
). If it needs to also communicate with processes outside of the host it's running on, then you'll want to open an internet socket (seeSocket.tcp_server_loop
).The general recipe for the server is:
On the client:
Your server and client(s) need to agree on who sends what first and what the appropriate responses are by the other party.