Forum: Ruby Win32-service: error 5: access denied

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Michael D. (Guest)
on 2007-07-11 12:20
hello altogether!

I have written an win32 service like explained in O'Reilys "Ruby
Cookbook" on page 750.

when i try to start the service is get the following error:
Zugriff verweigert (Win32::ServiceError)
or when i try to start the service manually:
Fehler 5: Zugriff verweigert
(in english: error 5: access denied)

does anyone know how to fix this?

thnx! have a nice day!
Michael
Michael D. (Guest)
on 2007-07-12 18:42
does nobody have any information?

thnx!
Jano S. (Guest)
on 2007-07-12 18:55
(Received via mailing list)
On 7/11/07, Michael D. <removed_email_address@domain.invalid> wrote:
>
> does anyone know how to fix this?

What system do you have? XP/W2k/vista?

Is the service already registered? (Can you see it in services?)
Can you start it with net start <name>?
Are you admin? Uberadmin in vista?
Can you start/stop other services?

Could you post the code you are trying to start?
Could you post the actual command (what did you do when you tried to
start the service)?

What do you do in your service?
You cannot use stdin, stdout, stderr; You don't see mapped/subst-ed
disks, You might not have rights to some files - you are most probably
running under SYSTEM user.

(Now I see this looks like a confession recipe ;-)

J.
Michael D. (Guest)
on 2007-07-12 19:00
Hi Jano!

thnx for post!!

I use winXP, sp2.
Yeah service is registered. in "services" the status is "manual"
Yes im Admin an yes i can start other Services.

so here is my code:
******************************************************
require 'rubygems'
require 'win32/service'
include Win32

SERVICE_NAME = "cProjectsOutlookEventsListener3"
SERVICE_DISPLAYNAME = "cProjectsOutlookEventsListener3"

if ARGV[0] == "register"
  #startet den service
  svc = Service.new
  svc.create_service{ |s|
    s.service_name = SERVICE_NAME
    s.display_name = SERVICE_DISPLAYNAME
    s.binary_path_name = 'c:\ruby\bin ' + File.expand_path($0)
    s.dependencies = []
  }
  svc.close
  puts "Service registriert: "+SERVICE_DISPLAYNAME

elsif ARGV[0] == "start"
  Service.start(SERVICE_NAME)
  puts "Service gestartet"

elsif ARGV[0] == "delete"
  #stop service
  if Service.status(SERVICE_NAME).current_state == "running"
    Service.stop(SERVICE_NAME)
  end
  Service.delete(SERVICE_NAME)
  puts "Service gelöscht: " + SERVICE_NAME

else
  # schreibe parameterinfo
  puts "Use: ruby outlook_rubysvc.rb [option]"
  puts "     options:"
  puts "             register - Registriert und starte den Service"
  puts "             delete   - Stoppt und löscht den Service"
  exit
end

# hier ist der eigentliche Service-code
class Daemon
  def service_init
    # Wartezeit bis der Service richtig initialisiert worden ist
    sleep 10
  end

  def service_main
    fileCount = 0
    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

  d = Daemon.new
  d.mainloop
end


******************************************************
thnx! Michael
Jano S. wrote:
> On 7/11/07, Michael D. <removed_email_address@domain.invalid> wrote:
>>
>> does anyone know how to fix this?
>
> What system do you have? XP/W2k/vista?
>
> Is the service already registered? (Can you see it in services?)
> Can you start it with net start <name>?
> Are you admin? Uberadmin in vista?
> Can you start/stop other services?
>
> Could you post the code you are trying to start?
> Could you post the actual command (what did you do when you tried to
> start the service)?
>
> What do you do in your service?
> You cannot use stdin, stdout, stderr; You don't see mapped/subst-ed
> disks, You might not have rights to some files - you are most probably
> running under SYSTEM user.
>
> (Now I see this looks like a confession recipe ;-)
>
> J.
Jano S. (Guest)
on 2007-07-12 20:02
(Received via mailing list)
On 7/12/07, Michael D. <removed_email_address@domain.invalid> wrote:
> require 'rubygems'
> require 'win32/service'
> include Win32
>
> SERVICE_NAME = "cProjectsOutlookEventsListener3"
> SERVICE_DISPLAYNAME = "cProjectsOutlookEventsListener3"

Offtopic: You can write the following as

case ARGV[0]
when "register"
...
when "start"
...
else
...
end

> if ARGV[0] == "register"
>   #startet den service
>   svc = Service.new
...
> # hier ist der eigentliche Service-code
> class Daemon
>   def service_init
>     # Wartezeit bis der Service richtig initialisiert worden ist
>     sleep 10
>   end

Try this: (the only possible place where it can raise "access denied"
exception is when you rename the file (I suppose you can't do that
while it's open). If it does not help, create a log file and store the
exception stack trace to it (i.e. enclose everything in begin /
rescue.

>   def service_main
-     fileCount = 0
+     fileCount = 0
>     watchForFile = "C:\findme.txt"
>     while state == RUNNING
>       sleep 5
>       if File.exists? watchForFile
-         fileCount += 1
-         File.rename watchForFile, watchForFile + "." + fileCount.to_s
+        begin
+          File.rename watchForFile, watchForFile + "." + fileCount.to_s
+          fileCount += 1
+         rescue
+         end
>       end
>     end
>   end

Of course, I assume the program works when run standalone, from the
cmd.exe.

Access permissions of NTFS might be in the game as well. You may try
playing with runas /user:SYSTEM, too.

J.
This topic is locked and can not be replied to.