Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


November 2006

The All-Purpose Service Controller

Use SC as a more fully functioning Net command alternative
RSS
Subscribe to Windows IT Pro | See More Administration Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Command-line junkies love the ability to manipulate Windows services from the command prompt, but until a few years ago all we had was Net Start, Net Pause, Net Continue, and Net Stop, which—as their names suggest—start, pause, continue (i.e., "un-pause"), and stop a service.

If you'd like to be able to do more with services from the command line, you should check out the SC tool (sc.exe), which you'll find in Windows XP and later.

What Does It Look Like?
SC commands share the structure

sc [\\<servername>] <command> <servicename> [<options>] 

where the optional servername simply points to the system at which to direct the command, command is the particular SC subcommand (e.g., pause, start, create, delete), servicename is the service's internal name, and options refers to any options that the subcommand requires. The simple example

sc \\server1 stop lanmanserver 

contacts the Server1 system and stops the Server service, which goes by the internal service name lanmanserver. If I wanted to stop the Server service locally, I'd simply drop the servername option.

The Internal Name
Knowing the service's internal name is essential. If I assume that the displayed name for the file-server service is Server, and I try the command

sc stop server 

I get an SC error. Only lanmanserver works. The internal name is also called the key name because lanmanserver is the name of the registry subkey within HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services in which Windows stores information about that particular service. If you know the friendlier display name— such as Server for the file-server service or DNS for the DNS Server service—just use the getkeyname command to convert a display name to a key name. For example, to find the DNS Server service's key name, I could use the command

sc getkeyname "dns server" 

Doing so tells me that the DNS Server service's key name is DNS. (Case seems not to matter to SC, by the way.) You can reverse the process, and—given the key name—get the more human-friendly display name, by using the getdisplayname command.

How Does It Work?
SC has four commands that correspond to the aforementioned Net commands: start, stop, pause, and continue. For example, to pause the server service, I could type

sc pause server 

SC's "stop" functionality behaves a little differently than it does with the Microsoft Management Console (MMC) Services snap-in or the Net Stop command, which understand that the Computer Browser service depends on the Server service. The Services snap-in and Net Stop ask if it's OK to stop the Computer Browser service as well, whereas SC just fails with the statement A stop control has been sent to a service that other running services are dependent on. In other words, in this instance, SC isn't quite as smart as the other commands.

With the enumdepend command, however, you can determine what other services depend on a given service:

sc enumdepend lanmanserver 

In response, SC reports that the DFS and Browser services depend on the Server service. But SC returns this information in a sort of terse, techie way. For example, you can use the query command to query a service about its current state. Querying the Server service with

sc query lanmanserver 

returns the information that Figure 1 shows. This is the basic way that SC lists information about a service.

SC is a pretty useful tool, but I've only just begun. Join me next month for more SC!

End of Article



Reader Comments
SC is a great tool, but I've noticed something that has an effect on shell scripting. SC sends start, stop, pause, and continue signals and doesn't wait for completion of the task before exiting. If type "SC Stop service" and then "sc query service" you will see a "STOP_PENDING" state. This means the following command in a shell script could run before the service is completely stopped.

How do I handle that situation? I have a service I want to stop then backup the files to another location using NTBACKUP from the command line. I want to script this and I want wait till the service is completely stopped before running the backup command, wait till the backup is complete before starting the service again.

kincaid@valleysupply.net January 26, 2007 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

WinInfo Short Takes: Week of November 9, 2009

An often irreverent look at some of the week's other news, including some more Windows 7 sales momentum, some Sophos stupidity, Microsoft's cloud computing self-loathing, more whining from the browser makers, Zoho's "Fake Office," and much, much more ...

Where is Microsoft NetMeeting in Windows XP?

...


Related Articles Castaway on Command-Prompt Island

Related Events WinConnections and Microsoft® Exchange Connections

Deep Dive into Windows Server 2008 R2 presented by John Savill

Check out our list of Free Email Newsletters!

Windows OSs eBooks Understanding and Leveraging Code Signing Technologies

A Guide to Windows Certification and Public Keys

SQL Server Administration for Oracle DBAs

Related Windows OSs Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement