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
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
mkfifo ./my_pipe
Step 2: Create your "daemon":
File.open('my_pipe', 'r+') do |f|
loop do
line = f.gets
puts "Got: #{line}"
end
end
and run it.
Step 3: Open another terminal and run
cat > my_pipe
and start typing some text, line by line.
Step 4: Watch output of the daemon.
Step 5: ???
Step 6: Profit.