Services Overview

Microsoft® Windows® 2000 Scripting Guide

Services are an important part of the Microsoft® Windows® 2000 operating system. A service, which is similar to the daemons used in the UNIX operating system, is an application that can communicate with and be administered by the Service Control Manager (SCM). In addition, a service can:

  • Automatically start each time a computer starts.

  • Run when no user is logged on to the computer. In fact, services can run even if no user ever logs on to the computer.

  • Respond to requests without human intervention.

  • Be configured to automatically restart if it fails.

These capabilities not only make services vitally important to the way computers function, but also make system administration viable. For example, without the DHCP service, an administrator would have to manually configure each computer's IP address. Without the DNS service, an administrator would have to manually configure and maintain Hosts or Lmhosts files. Without the automated capabilities of services, these tasks simply could not be performed in an enterprise setting.

Because services play such an important role in an organization's computing infrastructure, service management is a crucial part of any system administrator's job. If a service stops functioning, it affects the computer on which the service runs. Depending on the service, however, it could also adversely affect many users and their computers. If the DHCP service fails, computers are not given IP addresses, and users lose network connectivity. If the DNS service fails, the Active Directory® directory service is unavailable, and users cannot locate domain resources.

Managing services helps ensure that:

  • Computers can fill their roles as workstations, domain controllers, mail servers, and database servers. For example, if the mail service stops, a mail server cannot carry out its assigned function.

  • Users have access to resources both on the local computer and throughout the network.

  • Disruptions to the workplace are minimized. By monitoring services, you can be alerted the moment a problem occurs. For example, if the mail service stops, the service failure can be immediately detected and the mail service restarted, even before users begin to experience problems.

How Services Work

Services, like other applications, are run from executable files. For example, the DNS service runs in an instance of systemroot\system32\dns.exe, and Internet Information Services runs in an instance of systemroot\ system32\inetsrv\inetinfo.exe.

However, services are a special type of application. Unlike the executable files for most applications, the executable file for a service includes code that enables it to perform the special functions of a service and to communicate with the SCM.

To run an application as a service, the following components (some part of the operating system, some part of the individual service) must be present.

  • Service Control Manager. Communicates with services by forwarding commands that request a service to start, stop, pause, or resume. The SCM also monitors the status of each installed service and carries out specific actions if a service fails. (A failure is defined as any time a service stops without first sending the appropriate stop code to the SCM.)

    The SCM provides a unified means for configuring and managing services. In particular, the SCM does the following:

    • Maintains a database of all services.

    • Starts services either during system startup or on demand.

    • Enumerates installed services.

    • Maintains status information for all services.

    • Transmits commands (control requests) to services.

    • Locks and unlocks the service database.

  • Executable file for each service. Includes code that enables the service to respond to commands from the SCM and to communicate its status to the SCM. This requirement precludes most applications from running as a service; Notepad, for example, cannot run as a service because the developers did not include code that enables Notepad to communicate with the SCM.

  • Service control program. Allows a user to communicate with the SCM, which in turn communicates with an individual service. For example, to modify a service, you use a service control program (SCP) to send the modification commands to the SCM. In turn, the SCM relays those commands to the service, and the appropriate modifications are made.

    The Services snap-in to the Microsoft Management Console (MMC) is an example of a service control program, as are Sc.exe and the Net Start and Net Stop commands. Windows Management Instrumentation (WMI) scripts can also function as service control programs.

Running Services in Windows 2000

When a Windows 2000-based computer starts, the executable file for SCM (Services.exe) begins running before the logon dialog box appears. This allows autostart services to start before any user logs on.

After Services.exe starts, the SCM scans the contents of the registry subkey HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services. For each service listed in the registry, the SCM creates a corresponding entry in the services database. It establishes channels for communicating with service control programs and then starts the autostart services.

When a service starts, it initializes a minimum of two threads. One thread is used to communicate with the SCM and the other is used to respond to requests from client applications. For example, a Web server service creates a TCP "listen socket" and waits for inbound HTTP requests. When such a request is received, the thread becomes active, processes the request, and then suspends itself until the next request arrives. An application that initializes only a single thread cannot run as a service.