Wednesday, April 2, 2008

Starting and stopping Windows Service from the command line

In this article I will show how to restart (stop then start) a Windows Service from the command line. While it is true you can do all this from the user interface using the mouse, sometimes it is useful to be able to script restarting of services if a service is hung or down for some reason. An example of this is Apache Tomcat (assuming you installed it as a Windows Service and not to run from the command line to start with. I am going to use Apache Tomcat Windows Service as an example, but this article should apply to any Windows Service.

The below assumes you have already started a command prompt by going to Start Menu | Run… and typed cmd and then enter.

List Services

First the first thing you may want to know how to do is get a list of all started Windows services.

C:\>net start

These Windows services are started:

Apache Tomcat

Application Layer Gateway Service

Automatic Updates

Background Intelligent Transfer Service

COM+ Event System

Cryptographic Services

….

Stop a Service

Running the command below will stop the Apache Tomcat service. It is important to note the use of double-quotes here. You need them so that Apache Tomcat is treated as one parameter instead of two. The same goes for any other service with a space in its name. Also, notice the name use with the start command must match what is shown in the list above.

C:\>net stop “Apache Tomcat”

The Apache Tomcat service was stopped successfully.

Start a Service

Running the command below will start the Apache Tomcat service. The same basic rules apply as when you stopped the service.

C:\>net start “Apache Tomcat”

The Apache Tomcat service is starting.

The Apache Tomcat service was started successfully.

Restart a Service

Running the command below will restart (stop and immediately start) the Apache Tomcat service. The same basic rules apply as when you stopped the service.

C:\>net stop “Apache Tomcat”

The Apache Tomcat service was stopped successfully.

C:\>net start “Apache Tomcat”

The Apache Tomcat service is starting.

The Apache Tomcat service was started successfully.

9 comments:

Jeremy Stein said...

Did you mean "Apache Tomcat" rather than "Tomcat Apache"?

Brent V said...

Jeremy,

You are right! I did mean "Apache Tomcat" instead of "Tomcat Apache" in the example commands. Good find, and thank you for letting me know. I have made the correct.

Thanks,

Brent

Anonymous said...

this blog saved my butt tonight!
thanks a ton

Anonymous said...

Modern versions of Windows of course, will give the following error instead:

System error 5 has occurred.

Access is denied.

Anonymous said...

Good one. Thanks for sharing.

Tomer Bar-Shlomo said...

Very useful, thank you Jeremy.

Anonymous said...

Very Useful, Thanks a lot..

Anonymous said...

How do you get by the System error 5 problem ?

Anonymous said...

System error 5 has occurred.

Access is denied.

Solution: Open Command Prompt with run as Administrator to resolve the issue.