Best Practices Microsoft

Manage OS Triggered Services effectively in Windows 7 using sc.exe

We all work with Windows applications which have interactive system services to handle.  Sometimes we tend to mend the registry keys to configure the service settings, which is never a Best Practice!!.

Sc.exe is a Service Management Utility which solves this purpose. It is a command line program which is primarily used for communicating with the Service Control Manager (SCM) and the services.  With this utility, you can do the below operations with ease.

  1. Query the status of a service
  2. Create a service using a command line
  3. Start, pause, continue and stop a service
  4. Interrogate control requests to a service
  5. Change the configuration of a service
  6. Change the actions taken by the service upon failure
  7. Query the service SID type of a service
  8. Queries the triggered parameters of a services (Triggered Services is a model implemented in Windows 7)
  9. Enumerates service dependencies
  10. You can also lock the SCM database

The general usage of this command would be.,

sc <server> [command] [service name] <option1> <option2>..

Developers and administrators should never edit the registry to configure service settings. Instead, they should use sc.exe, the services MMC snap-in, or the SCM client API.

Few Examples Usages:

By using a triggerinfo switch we can get the status of a trigger service “browser” and its status, along with the port configurations


In this example, we just use the query switch which displays the type, state, exit code, checkpoint and the wait_hint for the “bits” service.

Windows also includes an MMC snap-in—services.msc—that provides basic SCM functionality through a graphical user interface.


Windows Services and SCM Improvements in Windows 7

Windows Services are designed to run as noninteractive system processes in the background. Services respond to application and user requests and manage devices or other system resources. Specifically, a Windows service is an application that is implemented by using the Windows Services programming model. Windows 7 supports the ability to start or stop a service when specific event triggers occur. The OS also includes significant improvements to the SCM.

Types of Windows Service Startup:

  1. Automatic specifies that a service should be started immediately after the system has completed starting the operating system.
  2. Automatic (Delayed Start) specifies that a service should be started approximately 2 minutes after the system has completed starting the operating system.
  3. Manual specifies that a service can be started at any time during regular system operation or whenever a dependent service is started. In Windows 7 supports the ability to start or stop a service when specific event triggers occur., such as the addition of a particular hardware device.

SCM Improvements in Windows 7:

The Windows 7 operating system includes significant improvements to the SCM. These improvements are designed to allow services to be started only when they are required and to help reduce the number of services that use Automatic Start. To realize the vision of having services run only when they are absolutely required, the Windows SCM infrastructure was improved in Windows 7.

Service developers should be aware of these improvements so that they can design their services to start on demand. The following section describes the improvements to the Windows SCM infrastructure for starting services only when they are required and provides guidance for developers on how to take advantage of these improvements to optimize the efficiency of their services.

The improvements in Windows 7 are designed to enable:

  1. Demand Start of a service when a specific event occurs. A service can register to be started or stopped on a supported event. These events are known as triggers. Starting a service that is based on a trigger is called a Trigger Start.
  2. Demand Start of a service by using SCM client API or COM service activation. A service can also be started on demand by calling an SCM client API function or by COM service activation when a client application requires functionality that is provided by the service. The infrastructure that supports this functionality was improved in Windows 7.
  3. Stopping a service when idle: A service can stop itself if it determines that it is not servicing any clients or performing any other activity for a specified amount of time.

Command Line Utility (Sc.exe):

Windows includes a command-line utility, Sc.exe is the SCM command-line tool in Windows that lets you create, delete, start, or stop a service, as well as query or modify the configuration of a service.

You can use the command “sc qtriggerinfo” to view service triggers.

Note: Moving to Demand Start or Trigger Start may not be appropriate for all services. For example, a service that requires activity during most of the system’s uptime or that has a high frequency of intermittent activity should not move to a Demand Start or Trigger Start model. In these situations, it is appropriate to use Automatic Start.