Sunday, April 09, 2006

Get NT services by their Horns

Command line interface for windows users is highly unlikely. Windows is not designed for such. One will not doubt that using command line interface is very efficient compared to GUI counterpart. Remember our mighty Linux!! . Especially I don't like to start services.msc whenever I need to start/stop/restart some services, like I have installed apache, mysql as services and I need to start/stop them ferquently. That's when I get to understand the use of CLI in dealing with NT services.

If you know the service name, you can use net start/net stop
>net start servicename
>net stop servicename

To find the servicename, start services from MMC(Microsoft Management Console) or type services.msc at the start->run and enter.

However this requires you to open GUI, which indubitably consumes RAM. There's another utitity sc, which is defined in its help as

SC is a command line program used for communicating with the NT Service Controller and services.

USAGE:
sc <> [command] [service name] <> <>...

The option <> has the form "\\ServerName"
Further help on commands can be obtained by typing: "sc [command]"
Commands:
query-----------Queries the status for a service, or
enumerates the status for types of services.
queryex---------Queries the extended status for a service, or
enumerates the status for types of services.
start-----------Starts a service.
pause-----------Sends a PAUSE control request to a service.
interrogate-----Sends an INTERROGATE control request to a service.
continue--------Sends a CONTINUE control request to a service.
stop------------Sends a STOP request to a service.
config----------Changes the configuration of a service (persistant).
description-----Changes the description of a service.
failure---------Changes the actions taken by a service upon failure.
qc--------------Queries the configuration information for a service.
qdescription----Queries the description for a service.
qfailure--------Queries the actions taken by a service upon failure.
delete----------Deletes a service (from the registry).
create----------Creates a service. (adds it to the registry).
control---------Sends a control to a service.
sdshow----------Displays a service's security descriptor.
sdset-----------Sets a service's security descriptor.
GetDisplayName--Gets the DisplayName for a service.
GetKeyName------Gets the ServiceKeyName for a service.
EnumDepend------Enumerates Service Dependencies.

The following commands don't require a service name:
sc <> <> <>
boot------------(ok | bad) Indicates whether the last boot should
be saved as the last-known-good boot configuration
Lock------------Locks the Service Database
QueryLock-------Queries the LockStatus for the SCManager Database


With sc in hand, you can do anything with services as that in case of GUI.

Let's say you want to see all the running services.
>sc query
Our eyes certainly don't allow us to see at such speed. You may use the following to send output to the file and look at the running services taking your time.

>sc query > c:\runningservices.txt

A file named runningservices.txt will be created at c:\. A sample of one of those services looks something like this

SERVICE_NAME: ALG
DISPLAY_NAME: Application Layer Gateway Service
TYPE : 10 WIN32_OWN_PROCESS
STATE : 4 RUNNING
(STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0

runningservices.txt in my c:\ is almost 380 lines. Only the servicename or display name would have sufficed. findstr came to my rescue.

Piping (usage of |) is popular among linux users, with which you can feed the output of one command to next command . I am happy to see the Microsoft also has provision for pipes in its CLI.

Now to get the services names only, try this
>sc query | findstr /I service_name

/I will make findstr case-insensitive.
Type findstr /? for more options

I hope you can now send the output to file easily. Incase of confusion, try this
>sc query | findstr /I service_name > c:\runningservicesonly.txt

To get the names of all the services
>sc query state= all | findstr /I service_name
Note there's no space between space and =; and there's space between = and all.
I don't understand why MS compelled us to do this.

To get display names of inactive or stopped services
>sc query state= inactive | findstr /I display_name


Like net start and stop, you can also start/stop the services using sc

Let's say you know there should be service called apache but you are not sure about the service name and You need it to start or stop the service. Please forget about services.msc. I am trying to focus on CLI.

I typed the following command
>sc query state= all | findstr /I apa
The output being
SERVICE_NAME: Apache2
DISPLAY_NAME: Apache2

Now the service Apache2 can be started using
>sc start apache2

To ensure whether it is running or not
>sc query apache2

For extra details,
>sc queryex apache2

To stop
>sc stop apache2

Let's say you delete the apache folder manually but forget to run the script that deletes apache service from MMC. Don't worry if that happens to you, with sc at hand, you can even outrun GUI.
>sc delete apache2

I don't think NT services have horns to get them by, as title says you to. But I hope you are in a position to get them without mouse. Happy getting serviced.

I tried the above commands in Windows XP (SP2). I haven't tried sc in other flavors of windows 2000, XP and 2003. Please find yourself and let others know in case.

No comments: