Win32 Service Problem

G

Glen Holcomb

[Note: parts of this message were removed to make it a legal post.]

I'm having an issue with Win32 Service

I have a simple script that I want to run as a service however the service
won't start (timeout issue). I've tried several things but nothing I do
seems to make any difference at all.

Here is the code:

begin
require 'rubygems'
require 'win32/service'
require 'chronic'

include Win32

class Daemon
def service_init
test_log = File.new("path_to_where_it_goes", "w")
test_log.puts "got to init"
test_log.close
end

def service_main
while running?
if Time.now < Chronic.parse('today 10:00pm')
sleep(Chronic.parse('today 10:00pm') - Time.now)
`shutdown /s /t:60`
else
sleep(Chronic.parse('tomorrow 10:00pm') - Time.now)
`shutdown /s /t:60`
end
end
end
end
rescue Exception
logfile = File.new("path_to_logfile", "w")
logfile.puts $!
logfile.close
end

Daemon.mainloop

I get absolutely no feedback from the script it's self. In the even log it
says the service didn't respond within 30 seconds. Oddly enough it decides
this after a second or two. I've tried the script with different code in
the service_init method and without a service_init method it doesn't seem to
make any difference.

I realize this would be easier with a task but for reasons beyond my control
the task scheduler is turned off on all classroom and lab computers.
 
D

Daniel Berger

I'm having an issue with Win32 Service

I have a simple script that I want to run as a service however the service
won't start (timeout issue). I've tried several things but nothing I do
seems to make any difference at all.

Here is the code:

begin
require 'rubygems'
require 'win32/service'
require 'chronic'

include Win32

class Daemon
def service_init
test_log = File.new("path_to_where_it_goes", "w")
test_log.puts "got to init"
test_log.close
end

def service_main
while running?
if Time.now < Chronic.parse('today 10:00pm')
sleep(Chronic.parse('today 10:00pm') - Time.now)
`shutdown /s /t:60`
else
sleep(Chronic.parse('tomorrow 10:00pm') - Time.now)
`shutdown /s /t:60`
end
end
end
end
rescue Exception
logfile = File.new("path_to_logfile", "w")
logfile.puts $!
logfile.close
end

Daemon.mainloop

I get absolutely no feedback from the script it's self. In the even log it
says the service didn't respond within 30 seconds. Oddly enough it decides
this after a second or two. I've tried the script with different code in
the service_init method and without a service_init method it doesn't seem to
make any difference.

I realize this would be easier with a task but for reasons beyond my control
the task scheduler is turned off on all classroom and lab computers.

There's no shell associated with a service, so doing backticks or
system commands in a service is a Bad Idea. Also, as of version 0.6.0
STDIN, STDOUT and STDERR are redirected to NUL by default, so you'll
have to reopen STDERR to a log file if you want to see the full output
(what you saw there happened before they were redirected).

My recommendation is not to use backticks but to use WMI +
Win32_OperatingSystem via OLE. You can find details at:

http://msdn.microsoft.com/en-us/library/aa394239(VS.85).aspx

Regards,

Dan
 
G

Glen Holcomb

[Note: parts of this message were removed to make it a legal post.]

Thanks, I'll give it a shot.
 
G

Glen Holcomb

[Note: parts of this message were removed to make it a legal post.]

Okay now I'm getting undefined method mainloop for Daemon.

I'll try re-installing the gem but this seems a bit odd to me.
 
D

Daniel Berger

Okay now I'm getting undefined method mainloop for Daemon.

I'll try re-installing the gem but this seems a bit odd to me.

You can fire up irb and check your version.

p Win32::Service::VERSION

You'll want 0.6.0 or later.

Regards,

Dan
 
G

Glen Holcomb

[Note: parts of this message were removed to make it a legal post.]

Well I'm running 0.6.1

I tried going back to 0.5.2 as that had worked for me in the past but no
luck. I went back to 0.6.1 again and still the same error.
 
G

Glen Holcomb

[Note: parts of this message were removed to make it a legal post.]

I tried a clean install of Ruby and everything on a machine that was
recently set up. I still get the same error so I'm pretty much out of
ideas.
 
D

Daniel Berger

I tried a clean install of Ruby and everything on a machine that was
recently set up. =A0I still get the same error so I'm pretty much out of
ideas.

Try Daemon.new.mainloop.

However, I'm still very confused by this. Try logging the version
number to a file to make sure you're using the version you think
you're using.

Regards,

Dan
 
G

Glen Holcomb

[Note: parts of this message were removed to make it a legal post.]

Nope, same error message.

Here is the current code:

require 'rubygems'
require 'win32/service'
require 'chronic'
require 'win32ole'

include Win32

$stderr.reopen("C:\\Documents and
Settings\\itstaff\\Desktop\\sandman_error.log", "w")
logfile = File.new("C:\\Documents and
Settings\\itstaff\\Desktop\\sandman_log.txt", "w")
logfile.puts Win32::Service::VERSION
logfile.close

class Daemon
def service_init
mgmt =
WIN32OLE.connect("winmgmts:{impersonationLevel=impersonate,(Shutdown)}//")
wmi_results = mgmt.ExecQuery("select * from Win32_OperatingSystem")
end

def service_main
while running?
if Time.now < Chronic.parse('today 10:00pm')
sleep(Chronic.parse('today 10:00pm') - Time.now)
for target in wmi_results
target.Win32Shutdown(1)
end
else
sleep(Chronic.parse('tomorrow 10:00pm') - Time.now)
for target in wmi_results
target.Win32Shutdown(1)
end
end
end
end
end

Daemon.new.mainloop

I wasn't sure about the scope with service_init and service_main was going
to figure that out later, but it won't even get that far.

The version output from this file is 0.6.1
 
D

Daniel Berger

Nope, same error message.

Here is the current code:

require 'rubygems'
require 'win32/service'
require 'chronic'
require 'win32ole'

<snip>

Add an explicit require 'win32/daemon' here and let me know if that
helps.

Regards,

Dan
 
G

Glen Holcomb

[Note: parts of this message were removed to make it a legal post.]

<snip>

Add an explicit require 'win32/daemon' here and let me know if that
helps.

Regards,

Dan
That did the trick. Thanks Dan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top