Running a Ruby Program as a Windows Service?

前端 未结 5 766
不思量自难忘°
不思量自难忘° 2020-12-02 05:47

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 c

5条回答
  •  猫巷女王i
    2020-12-02 06:03

    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

    Update 2012-11-20: According to https://stackoverflow.com/users/1374569/paul the register_bar.rb should now be

    Service.create( :service_name => 'some_service',
                    :host => 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'       )
    

    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

提交回复
热议问题