3 subcommands give you greater SC control
Last month, in “Taking 3 Swings at SC” (InstantDoc ID 93849), I showed you how to use Windows Server 2003's SC (sc.exe) command to create a new service based on a fictional service application (i.e., C:\wc\wcmail.exe) that grabs pictures from a Web cam every few minutes. I walked you through the process of specifying a service account for the service, setting it to autostart, suppressing error messages, and giving it a display name of “Web cam image mailer.” As you'll recall, the command we ended up with was
sc create webimagemailer binpath= C:\wc\wcmail.exe start= auto displayname= “Web cam image mailer” obj= .\webcamguy password= swordfish error= ignore
This month, let's go a little deeper. Suppose Webimagemailer can't run until the Windows Image Acquisition (WIA) service—with the key name stisvc—is running. In services terms, that would mean that Webimagemailer has stisvc as a dependency. To instruct Webimagemailer to wait until stisvc starts before starting itself, you would add the depend= stisvc parameter. (Don't forget: SC requires a space between the equals sign and the parameter value.) To specify that a service depends on more than one service, you would list the services' key names, separated by a forward slash. For example, to create the Webimagemailer service and specify that the stisvc and webclient services need to be running before it can start, you'd type
sc create webimagemailer binpath= C:\wc\wcmail.exe start= auto displayname= “Web cam image mailer” obj= .\webcamguy password= swordfish error= ignore depend= stisvc/webclient
While we're on the topic of dependencies, you can use three SC subcommands—enumdepends, qc, and config—to query SC about them. To determine which services depend on a given service, you can type
sc enumdepend <servicekeyname>
Thus, to see which services depend on the Server service— which has the key name lanmanserver, you'd type
sc enumdepend lanmanserver
Running that command on my test Windows 2003 server, for example, reveals that Netlogon, Dfs, and the computer browser services depend on the Server service.
To accomplish the reverse and determine Server dependencies, you can use the qc subcommand, as in
sc qc lanmanserver
This command dumps nine lines of information about the service, one of which is DEPENDENCIES. (SC tends to shout.) If you run that command, you'll find that Server doesn't depend on any services. To see a service that has more than one dependency, try the command on the Netlogon service. You'll see that Netlogon requires both the Server and Workstation services running before it can start.
Sometimes, dependencies are more complex than merely one service needing another. For example, some services will start only if one of three other services has started. (All three needn't be running; any one of the three will do.) You can instruct Windows about such a dynamic by informing the system that a given service depends on a group of services. Windows has a number of these services, such as the SCSI CDROM Class, SCSI miniport, Parallel arbitrator, NetBIOSGroup, NDIS, and Primary Disk services, to name a few. You can see all the services and drivers in a group by typing
sc query type= service|driver|all group= <”groupname”>
For example, to see all the services and drivers in the Primary Disk service group, you'd type
sc query type= all group= “primary disk”
Case doesn't seem to matter in group names. You can add a service to a given group, or create a new service group, by adding the group= groupname command to the SC Create command or by using SC Config to modify a service's group membership. For example, to add Webimagemailer to a new group named “unimportant,” you'd type
sc config webimagemailer group= unimportant
As far as I can see, you can't put a service or driver in more than one service group.
You can also tell Windows that Webimagemailer shouldn't load without a particular group. To specify the fictional Webstartup group, you'd use the depends= webstartup parameter. To signal to Windows that Webstartup is a group—not another service—you'd prefix its name with a plus sign. For example, to reconfigure Webimagemailer to depend on the Webstartup startup group, you'd type
sc config webimagemailer depends= +webstartup
Now you've seen how to use dependencies and groups to more specifically control a service's load order. You can understand why I was pleased to discover SC a few years ago.