Is it possible to run a ruby application as a Windows Service? I see that there is a related question which discusses running a Java Application as a Windows Service, how can you do this with a Ruby application?
You can write (or download) a wrapper service. The wrapper can call the ruby.exe to execute your program. Same trick works for Java, VB, etc.
You should be able to accomplish this in IronRuby since you would have the .NET framework behind you.
Check out the following library: Win32Utils. You can create a simple service that you can start/stop/restart at your leisure. I'm currently using it to manage a Mongrel instance for a Windows hosted Rails app and it works flawlessly.
You can also use this example as a method of enabling your Ruby application to act as a Windows service: Running as a Windows Service .
Good luck.
When trying the Win32Utils one really need to studie the doc and look over the net before finding some simple working example. This seems to work today 2008-10-02:
gem install win32-service
bar.rb
create the application/daemon
LOG_FILE = 'C:\\test.log'
begin
require "rubygems"
require 'win32/daemon'
include Win32
class DemoDaemon < Daemon
def service_main
while running?
sleep 10
File.open("c:\\test.log", "a"){ |f| f.puts "Service is running #{Time.now}" }
end
end
def service_stop
File.open("c:\\test.log", "a"){ |f| f.puts "***Service stopped #{Time.now}" }
exit!
end
end
DemoDaemon.mainloop
rescue Exception => err
File.open(LOG_FILE,'a+'){ |f| f.puts " ***Daemon failure #{Time.now} err=#{err} " }
raise
end
bar.rb is the service but we must create and register first! this can be done with sc create some_service
but if we are going to use ruby and win32utils we should do a
register_bar.rb
require "rubygems"
require "win32/service"
include Win32
# Create a new service
Service.create('some_service', nil,
:service_type => Service::WIN32_OWN_PROCESS,
:description => 'A custom service I wrote just for fun',
:start_type => Service::AUTO_START,
:error_control => Service::ERROR_NORMAL,
:binary_path_name => 'c:\usr\ruby\bin\rubyw.exe -C c:\tmp\ bar.rb',
:load_order_group => 'Network',
:dependencies => ['W32Time','Schedule'],
:display_name => 'This is some service'
)
Note, there is a space between c:\tmp\ bar.rb in 'c:\usr\ruby\bin\rubyw.exe -C c:\tmp\ bar.rb'
Run ruby register_bar.rb
and now one can start the service either from the windows service control panel or
sc start some_service
and watch c:test.log be filled with Service is running Thu Oct 02 22:06:47 +0200 2008
For the simple of have something to work with it is easier to remove the service register and create a new one instead of modifying a existing one
unregister_bar.rb
require "rubygems"
require "win32/service"
include Win32
Service.delete("some_service")
Credits to the people http://rubypane.blogspot.com/2008/05/windows-service-using-win32-service-and_29.html