Question

I am using the win32-service gem to create a Windows service using Ruby (1.9.3-p429, MRI).

This snippet of code works.

require 'rubygems'
require 'win32/service'

include Win32

SERVICE_NAME = 'myservice'

# Create a new service
Service.create({
  :service_name        => SERVICE_NAME,
  :service_type       => Service::WIN32_OWN_PROCESS,
  :description        => 'A custom service I wrote just for fun',
  :start_type         => Service::AUTO_START,
  :error_control      => Service::ERROR_NORMAL,
  :binary_path_name   => 'c:\Ruby193\bin\ruby.exe -C c:\ c:\myservice.rb',
  :load_order_group   => 'Network',
  #:service_start_name => 'Administrator',
  #:password           => 'adminpasswd',
  :display_name       => SERVICE_NAME
})

Service.start SERVICE_NAME

The problem I have is that the service needs to run with Administrator privileges, but the entity which calls this code snippet runs as the Local System Account, and that is the default privilege.

I can open up up the Services GUI (services.msc) and go in and stop the service, raise the privileges via the "Log On" tab of the service (myservice) properties, and use Administrator/adminpasswd as the user/password. It then runs the service with sufficient privileges.

However, when I try to call Service.create with the :service_start_name and :password set to exactly the same values (by uncommenting the lines in the code snippet) as I used in the Services tab, it doesn't work. This server is an Amazon EC2 server running Windows 2008r2 Datacenter Edition and is not a part of any Windows domain that I know of (because I started it).

What do I need to do differently to get this Windows service running with Administrator privileges?

Was it helpful?

Solution 2

Borodin gave me a clue for this answer. When I went back to the Services GUI to reconfigure the service, I noticed that although I had typed 'Administrator' as the user name, the user name which actually showed up in the panel was '.\Administrator'. Keeping Borodin's comment in mind, it looks like I could specify '.' as the domain.

So...the code which actually worked was:

require 'rubygems'
require 'win32/service'

include Win32

SERVICE_NAME = 'myservice'

# Create a new service
Service.create({
  :service_name        => SERVICE_NAME,
  :service_type       => Service::WIN32_OWN_PROCESS,
  :description        => 'A custom service I wrote just for fun',
  :start_type         => Service::AUTO_START,
  :error_control      => Service::ERROR_NORMAL,
  :binary_path_name   => 'c:\Ruby193\bin\ruby.exe -C c:\ c:\myservice.rb',
  :load_order_group   => 'Network',
  :service_start_name => '.\Administrator',
  :password           => 'adminpasswd',
  :display_name       => SERVICE_NAME
})

Service.start SERVICE_NAME

OTHER TIPS

The underlying CreateService Windows API function requires an account domain on the lpServiceStartName field, so you probably need to set the :service_start_name field to 'domain\Administrator', where the account domain is usually the computer name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top