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 
ubygems
	require win32/service
	include Win32

	puts Currently Running 
Services:
	Service.services do |svc|
	 if svc.current_state == 
unning
	 puts "#{svc.service_name}	-	#{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)
	# => #

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 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

  • The win32-service library was written by Daniel J. Berger; its part of his win32utils project (http://rubyforge.org/projects/win32utils/)
  • The win32-service APIreference at http://rubyforge.org/docman/view.php/85/29/service.txt; see especially the member list for the Win32Service struct yielded by Service.services
  • You can also use win32-service to make your own services; see Recipe 20.2, "Creating a Windows Service"


Strings

Numbers

Date and Time

Arrays

Hashes

Files and Directories

Code Blocks and Iteration

Objects and Classes8

Modules and Namespaces

Reflection and Metaprogramming

XML and HTML

Graphics and Other File Formats

Databases and Persistence

Internet Services

Web Development Ruby on Rails

Web Services and Distributed Programming

Testing, Debugging, Optimizing, and Documenting

Packaging and Distributing Software

Automating Tasks with Rake

Multitasking and Multithreading

User Interface

Extending Ruby with Other Languages

System Administration



Ruby Cookbook
Ruby Cookbook (Cookbooks (OReilly))
ISBN: 0596523696
EAN: 2147483647
Year: N/A
Pages: 399

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net