Skip to content

Instantly share code, notes, and snippets.

@jmcarbo
Created August 12, 2009 19:57
Show Gist options
  • Save jmcarbo/166709 to your computer and use it in GitHub Desktop.
Save jmcarbo/166709 to your computer and use it in GitHub Desktop.
# rubysrvc.rb
require 'rubygems'
require 'win32/service'
require 'win32/daemon'
include Win32
SERVICE_NAME = "RubySvc"
SERVICE_DISPLAYNAME = "A Ruby Service"
if ARGV[0] == "register"
# Start the service.
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:\ruby\bin\ruby "' + File.expand_path($0) + '"',
:load_order_group => 'Network',
:dependencies => "",
:display_name => SERVICE_DISPLAYNAME)
#['W32Time','Schedule']
# :service_start_name => 'SomeDomain\\User',
# :password => 'XXXXXXX',
# Start the service.
# svc = Service.new(SERVICE_NAME)
# svc.create_service do |s|
# s.service_name = SERVICE_NAME
# s.display_name = SERVICE_DISPLAYNAME
# s.binary_path_name = 'C:\ruby\bin\ruby ' +
# File.expand_path($0)
# s.dependencies = []
# end
# svc.close
puts "Registered Service - " + SERVICE_DISPLAYNAME
elsif ARGV[0] == "delete"
# Stop the service.
if Service.status(SERVICE_NAME).current_state == "running"
Service.stop(SERVICE_NAME)
end
Service.delete(SERVICE_NAME)
puts "Removed Service - " + SERVICE_DISPLAYNAME
else
#if ENV["HOMEDRIVE"]!=nil
# We are not running as a service, but the user didn't provide any
# command line arguments. We've got nothing to do.
# puts "Usage: ruby rubysvc.rb [option]"
# puts " Where option is one of the following:"
# puts " register - To register the Service so it " +
# "appears in the control panel"
# puts " delete - To delete the Service from the control panel"
# exit
#
# end
# If we got this far, we are running as a service.
class Daemon
def service_init
# Give the service time to get everything initialized and running,
# before we enter the service_main function.
sleep 10
end
def service_main
fileCount = 0 # Initialize the file counter for the rename
watchForFile = "c:\\findme.txt"
while state == RUNNING
sleep 5
if File.exists? watchForFile
fileCount += 1
File.rename watchForFile, watchForFile + "." + fileCount.to_s
end
end
end
end
d = Daemon.new
d.mainloop
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment