Forked from theldoria/register_ruby_example_service.rb
Created
December 24, 2018 19:08
-
-
Save doytsujin/84debbb9805b32cf8d0387284839eadd to your computer and use it in GitHub Desktop.
ruby windows service example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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