Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Start and Stop Services Programmatically

Status
Not open for further replies.

Ambatim

Programmer
Feb 28, 2002
110
IN
Is there any way we can start and stop the Win XP services programmatically. Will it be possible to write a .cmd or .bat file, which can be executed and the command prompt, to start and stop the services

Thanks in advance
Mallik
 
Net services overview" is a Help and Support article.

Maybe you can make use of the "Net Stop" or "Net Start" command in a batch file?
 
Use sc.exe

Starts a service running.

Syntax
sc [ServerName] start ServiceName [ServiceArguments]

Parameters
ServerName
Specifies the name of the remote server on which the service is located. The name must use the UNC format ("\\myserver"). To run SC.exe locally, ignore this parameter.

ServiceName
Specifies the service name returned by the getkeyname operation.

ServiceArguments
Specifies service arguments to pass to the service to be started.

/?
Displays help at the command prompt.

Examples
The following example shows how you can use the sc start command:

sc start tapisrv

sc stop

Sends a STOP control request to a service.

Syntax
sc [ServerName] stop ServiceName

Parameters
ServerName
Specifies the name of the remote server on which the service is located. The name must use the UNC format ("\\myserver"). To run SC.exe locally, ignore this parameter.

ServiceName
Specifies the service name returned by the getkeyname operation.

/?
Displays help at the command prompt.

Remarks
Not all services can be stopped.

Examples
The following example shows how you can use the sc stop command:

sc stop tapisrv

 
Thank you bcastner.

How can I wait for the service to get started, before starting another service (to handle dependencies). I want to do something like this.

start Service A
loop
Check the status of service A
if the status of service A is started then
exit loop
end if
end loop
start Service B



Thanks in advance
Mallik
 
Start a registry editor (e.g., regedit.exe).
Navigate to the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ServiceB registry subkey.
Double-click DependOnService
Add ServiceA as a dependent service.


 
when I try to start a service using sc start ServiceA, it gives me this error

[SC] StartService: OpenService FAILED 1060:

The specified service does not exist as an installed service.

I have given the name whatever is visible under 'Display Name' in Services Window. I am able to start this service through the Services window, but not through a command script.

Any ideas ?

Thanks in advance
Mallik
 
Do an sc query and use the names that appear. You use the short names with the sc command.
 
My Service B is dependent on Service A.
I am using the the following commands in a script to stop these services.

sc stop Service B
sc stop Service A

The second command fails with the following error
[SC] ControlService FAILED 1051:
A stop control has been sent to a service that other running services are dependent on.


How can I wait 'in the script' for the Service B to stop completely, before issuing the command to stop Service A



Thanks in advance
Mallik
 
start /wait sc stop service B
start /wair sc stop Service A

Note:
The "start /wait" command runs the command (e.g. sc), then waits until the command actually terminates. Stopping and starting a service takes a while, so without this, the next command (e.g. sc stop Service A) may run before the sc stop Service B is actually stopped.
 
Typo, sorry:

start /wait sc stop service B
start /wait sc stop Service A

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top