views:

4315

answers:

4

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?

A: 

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.

Ken
A: 

You should be able to accomplish this in IronRuby since you would have the .NET framework behind you.

Geoffrey Chetwood
+11  A: 

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.

mwilliams
In regards to my comment, I stand corrected, I used a special Mongrel service gem and installed it like so: mongrel_rails service::install -N c:\web\AppName -p 4002 -e productionBut you should be able to accomplish what you need to with the above links.
mwilliams
+6  A: 

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

http://rubyforge.org/docman/view.php/85/595/service.html

Jonke
This doesn't work as written - I think it's based on an old version of win32/daemon. Try the example files here instead: http://rubyforge.org/frs/download.php/30036/win32-service-0.6.1.zip
Dave Nolan
This, as said, worked at the time recorded in the example. The win32 seems to change freq and I don't really feel the need to update this example every time.
Jonke
Yes, which is why I've downvoted it, sorry.
Dave Nolan
No problem with the donwvote but i think I would have prefered an aswer with your link, or really good, an update strip down example here, based on that link.
Jonke
This is a very nice and short tutorial on how to run Ruby service on Window, I will try it out later. It looks much easier than doing it with Python. Thanks for sharing it :)
Helen Neely