Win32 Service Problem

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.


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

On May 5, 6:24 pm, “Glen H.” [email protected] wrote:

require ‘win32/service’

end

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:

Regards,

Dan

Thanks, I’ll give it a shot.

On Mon, May 5, 2008 at 6:47 PM, Daniel B. [email protected]
wrote:

Here is the code:
test_log = File.new(“path_to_where_it_goes”, “w”)
sleep(Chronic.parse(‘tomorrow 10:00pm’) - Time.now)

make any difference.

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

Win32_OperatingSystem class - Win32 apps | Microsoft Learnhttp://msdn.microsoft.com/en-us/library/aa394239(VS.85).aspx

Regards,

Dan


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

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.

On Tue, May 6, 2008 at 6:59 AM, Glen H. [email protected]
wrote:

service

  while running?

rescue Exception
says the service didn’t respond within 30 seconds. Oddly enough it

http://msdn.microsoft.com/en-us/library/aa394239(VS.85).aspx
speak
so loud, I can’t hear a word you’re saying."

-Greg Graffin (Bad Religion)


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

On May 6, 10:40 am, “Glen H.” [email protected] wrote:

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

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.

On Tue, May 6, 2008 at 11:34 AM, Daniel B. [email protected]
wrote:

You’ll want 0.6.0 or later.

Regards,

Dan


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

On May 6, 1:25 pm, “Glen H.” [email protected] wrote:

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.

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

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.

On Tue, May 6, 2008 at 11:52 AM, Glen H. [email protected]
wrote:

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

-Greg Graffin (Bad Religion)


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

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

On Tue, May 6, 2008 at 1:45 PM, Daniel B. [email protected]
wrote:

number to a file to make sure you’re using the version you think
you’re using.

Regards,

Dan


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

On Tue, May 6, 2008 at 2:34 PM, Daniel B. [email protected]
wrote:

require ‘win32ole’

That did the trick. Thanks Dan.


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

On May 6, 2:14 pm, “Glen H.” [email protected] wrote:

Nope, same error message.

Here is the current code:

require ‘rubygems’
require ‘win32/service’
require ‘chronic’
require ‘win32ole’

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

Regards,

Dan