Google


   


You are here: CodeIdol.com > Other > Ruby Cookbook > System Administration > Managing Windows Services

SAVE
Digg
Shown on del.icio.us del.icio.us
See Whos Talking About This on Technorati Technorati
I've Reddit reddit

Recipe 23.2. Managing Windows Services

Credit: Bill Froelich

Problem

You want to interact with existing system services on the Windows platform.

Solution

User the win32-service library, available as the gem of the same name. Its Service module gives you an interface to work with services in Windows 2000 or XP Pro.

You can use this to print a list of the currently running services on your machine:

	require 'rubygems'
	require 'win32/service'
	include Win32

	puts 'Currently Running  
Services:'
	Service.services do |svc|
	  if svc.current_state == 'running'
	    puts "#{svc.service_name}\t-\t#{svc.display_name}"
	  end
	end
	# Currently Running Services:
	# ACPI    -      Microsoft ACPI Driver
	# AcrSch2Svc     -       Acronis Scheduler2 Service
	# AFD     -      AFD Networking Support Environment
	# agp440  -      Intel AGP Bus Filter
	# …

This command checks whether the DNS client service exists on your machine:

	Service.exists?('dnscache')                    # => true

Service.status returns a Win32ServiceStatus struct describing the current state of a service:

	Service.status('dnscache')
	# => #<struct Struct::Win32ServiceStatus
	#      service_type="share process", current_state="running",
	#      controls_accepted=["netbind change", "param change", "stop"],
	#      win32_exit_code=0, service_specific_exit_code=0, check_point=0,
	#      wait_hint=0, :interactive?=false, pid=1144, service_flags=0>

If a service is not currently running, you can start it with Service.start:

	Service.stop('dnscache')
	Service.status('dnscache').current_state       # => "stopped"
	Service.start('dnscache')
	Service.status('dnscache').current_state       # => "running"

Discussion

Services are typically accessed using their service_name attribute, not by their display name as shown in the Services Control Panel. Fortunately, Service provides helpful methods to convert between the two:

	Service.getdisplayname('dnscache')             # => "DNS Client"
	Service.getservicename('DNS Client')           # => "dnscache"

In addition to getting information about the status and list of services available, the win32-service gem lets you start, pause, and stop services. In the example below, replace the "foo" service with a valid service_name that responds to each of the commands.

	Service.start('foo')
	Service.pause('foo')
	Service.resume('foo')
	Service.stop('foo')

You can check whether a service supports pause or resume by checking the controls_ accepted member of its Win32ServiceStatus struct. As seen below, the dnscache command can't be paused or resumed:

	Service.status('dnscache').controls_accepted
	# => ["netbind change", "param change", "stop"]

Stopping system services may cause Windows to behave strangely, so be careful.

See Also


SAVE
Digg
Shown on del.icio.us del.icio.us
See Whos Talking About This on Technorati Technorati
I've Reddit reddit

You are here: CodeIdol.com > Other > Ruby Cookbook > System Administration > Managing Windows Services


ADBRITE ads links
   
Related tags







Popular Categories
Unix books and guides

AJAX popular information
C# language guides
Windows books and cookbooks

.......








Business Key Top Sites

be number one
rate your site




    С 2009 года мы стали переводить структура сайта на различные языки. Сайт теперь будет содержать книги не только на английском языке, но также и на других европейских языках, в том числе и на Русском языке.

    Русский Polski Francais Deutsch
    support sitemap terms

© CodeIdol Labs, 2007 - 2009