[Win32utils-devel] Issues with win32-service Daemon
win32utils-devel at rubyforge.org
win32utils-devel at rubyforge.org
Thu Apr 8 23:16:30 EDT 2004
Hi all,
I wrote a little echo client to run as a service. I have a couple of
problems with this. First, any time I try to define anything in
"initialize", the service fails to start. Any idea why?
Also, this service runs for a while, then just quits for no reason that I
can see. The Event Log merely says, "The Abba service terminated
unexpectedly".
Any ideas on either issue?
Dan
# myservice.rb
require "socket"
require "win32/service"
include Win32
class MyDaemon < Daemon
def service_main
File.open("c:\\test.log","a+"){ |f| f.puts("service_main entered") }
s = TCPServer.new(8888)
while socket = s.accept
rv = socket.gets.chomp
File.open("c:\\test.log","a+"){ |f| f.puts "GOT: #{rv}" }
socket.puts "Thanks!"
end
File.open("c:\\test.log","a+") { |f| f.puts("service_main left") }
#s.close
end
end
m = MyDaemon.new
m.mainloop
# run_service.rb
require "win32/service"
include Win32
Name = "AbbaSvc"
DisplayName = "Abba"
file = 'c:\eclipse\workspace\ruby_foo\service_stuff\myservice.rb'
bin = "ruby " + file
if Service.exists?(Name)
if Service.status(Name).current_state == "running"
Service.stop(Name)
sleep 3
puts "Stopped service"
end
n = 3
begin
Service.delete(Name)
rescue Win32::ServiceError => e
puts "Unable to delete: #{e}"
if n > 0
puts "Retrying..."
sleep 2
n -= 1
retry
else
STDERR.puts "Unable to delete. Exiting..."
exit
end
end
puts "Deleted service"
end
svc = Service.new
svc.create_service{ |s|
s.service_name = Name
s.display_name = DisplayName
s.binary_path_name = 'c:\ruby\bin\ruby ' + file
s.dependencies = []
}
svc.close
puts "Service installed. Attempting to start service"
sleep 2
Service.start(Name)
sleep 1
puts "Service started..."
_________________________________________________________________
Tax headache? MSN Money provides relief with tax tips, tools, IRS forms and
more! http://moneycentral.msn.com/tax/workshop/welcome.asp
More information about the win32utils-devel
mailing list