是否可以将ruby应用程序作为Windows服务运行?我看到有一个相关的问题讨论将Java应用程序作为Windows服务运行,如何使用Ruby应用程序执行此操作?
查看以下库:Win32Utils.您可以创建一个简单的服务,您可以在闲暇时开始/停止/重新启动.我目前正在使用它来管理Windows托管的Rails应用程序的Mongrel实例,它可以完美运行.
在尝试使用Win32Utils时,确实需要研究文档并查看网络,然后才能找到一些简单的工作示例.这似乎今天工作2008-10-02:
gem install win32-service
更新2012-11-20:根据/sf/ask/17360801/,register_bar.rb 现在应该是
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' )
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是服务,但我们必须先创建并注册!这可以通过sc create some_service来完成
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' )
请注意,'c:\ usr\ruby\bin\rubyw.exe -C c:\ tmp\bar.rb'中c:\ tmp\bar.rb之间有一个空格
运行ruby register_bar.rb
,现在可以从Windows服务控制面板启动服务
sc start some_service
并观看c:test.log充满服务正在运行Thu Oct 02 22:06:47 +0200 2008
require "rubygems" require "win32/service" include Win32 Service.delete("some_service")
给人们的信用 http://rubypane.blogspot.com/2008/05/windows-service-using-win32-service-and_29.html
http://rubyforge.org/docman/view.php/85/595/service.html