Skip to content

Instantly share code, notes, and snippets.

@pfeilbr
Created March 19, 2010 12:01
Show Gist options
  • Save pfeilbr/337454 to your computer and use it in GitHub Desktop.
Save pfeilbr/337454 to your computer and use it in GitHub Desktop.
ruby windows service example
require "rubygems"
require "win32/service"
require "pp"
include Win32
options = {:service_name=>'ruby_example_service',
: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:\Ruby\bin\ruby.exe -C c:\temp ruby_example_service.rb',
:load_order_group => 'Network',
:dependencies => ['W32Time','Schedule'],
:display_name => 'ruby_example_service'}
pp options
# Create a new service
Service.create(options)
#Service.delete('ruby_example_service')
LOG_FILE = 'C:\\test.log'
require "rubygems"
require 'sinatra'
class MyApp < Sinatra::Base
get '/' do
i = 0
10000.times { i = i*i }
'Hello world!'
end
end
begin
require 'win32/daemon'
include Win32
class DemoDaemon < Daemon
def service_main
MyApp.run! :host => 'localhost', :port => 9090, :server => 'thin'
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment