如何在 Windows 底下添加系統服務(system service)
以下程式碼呼叫方法:
註冊服務
ruby name.rb reg
註銷服務
ruby name.rb del
服務執行之程式碼位於 service_main 區塊內
註冊服務
ruby name.rb reg
註銷服務
ruby name.rb del
服務執行之程式碼位於 service_main 區塊內
require 'rubygems' require 'win32/service' require 'win32/daemon' include Win32 SERVICE_NAME = "RubyService" SERVICE_DISPLAY_NAME = "A Ruby Service by vegafish" if ( ARGV[ 0 ] == "reg" ) Service.create({ :service_name => SERVICE_NAME, :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:\\ruby187\bin\ruby.exe ' + File.expand_path($0), :load_order_group => 'Network', :dependencies => ['W32Time','Schedule'], :display_name => SERVICE_DISPLAY_NAME }) puts( "Service " + SERVICE_DISPLAY_NAME + " Registered." ) Service.start( SERVICE_NAME ) exit( 0 ) elsif ( ARGV[ 0 ] == "del" ) if Service.status( SERVICE_NAME ).current_state == "running" Service.stop( SERVICE_NAME ) end Service.delete( SERVICE_NAME ) puts( "Service " + SERVICE_DISPLAY_NAME + " Removed." ) exit( 0 ) end class Daemon def service_init sleep( 5 ) end def service_main filecount = 0 watchfile = "c:\\vegafish.txt" while( running?() ) sleep( 3 ) if ( File.exists?( watchfile ) ) filecount++ File.rename( watchfile, watchfile + filecount.to_s ) end end end end Daemon.mainloop()