I''m having some trouble separating the Daemon class stuff from the
Service Control stuff. I have a "daemon.rb" file in
''C:\''. I also have
a daemonctrl.rb script in another directory. It installs fine, but I
can never start it successfully. What am I doing wrong?
Dan
# daemon.rb in ''C:\''
require "win32/service"
include Win32
class Daemon
def service_stop
File.open("c:\\foo.log","a+"){ |f| f.puts "foo
stopped"
}
end
def service_main
while state == RUNNING
File.open("c:\\foo.log","a+"){ |f| f.puts "foo
running" }
sleep 5
end
end
end
d = Daemon.new
d.mainloop
# END daemon.rb
# daemonctrl.rb in ''C:\somedir''
require "win32/service"
include Win32
SERVICE_NAME = "FooSvc"
SERVICE_DISPLAYNAME = "Foo Service"
PROGRAM = "c:\\daemon.rb"
if ARGV[0]=="install"
svc = Service.new
svc.create_service{ |s|
s.service_name = SERVICE_NAME
s.display_name = SERVICE_DISPLAYNAME
s.binary_path_name = ''ruby '' + PROGRAM
s.dependencies = []
#s.start_type = Service::AUTO_START
}
svc.close
puts "installed"
elsif ARGV[0]=="start"
Service.start(SERVICE_NAME)
started = false
while started == false
s = Service.status(SERVICE_NAME)
started = true if s.current_state == "running"
break if started == true
puts "One moment, " + s.current_state
sleep 1
end
puts "Ok, started"
elsif ARGV[0]=="stop"
Service.stop(SERVICE_NAME)
stopped = false
while stopped == false
s = Service.status(SERVICE_NAME)
stopped = true if s.current_state == "stopped"
break if stopped == true
puts "One moment, " + s.current_state
sleep 1
end
puts "Ok, stopped"
elsif ARGV[0]=="uninstall" || ARGV[0] == "delete"
begin
Service.stop(SERVICE_NAME)
rescue
end
Service.delete(SERVICE_NAME)
puts "deleted"
elsif ARGV[0]=="pause"
Service.pause(SERVICE_NAME)
paused = false
while paused == false
s = Service.status(SERVICE_NAME)
paused = true if s.current_state == "paused"
break if paused == true
puts "One moment, " + s.current_state
sleep 1
end
puts "Ok, paused"
elsif ARGV[0]=="resume"
Service.resume(SERVICE_NAME)
resumed = false
while resumed == false
s = Service.status(SERVICE_NAME)
resumed = true if s.current_state == "running"
break if resumed == true
puts "One moment, " + s.current_state
sleep 1
end
puts "Ok, resumed"
end