Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test whether a service is running from the command line

I would like to be able to query whether or not a service is running from a windows batch file. I know I can use:

sc query "ServiceName"

but, this dumps out some text. What I really want is for it to set the errorlevel environment variable so that I can take action on that.

Do you know a simple way I can do this?

UPDATE
Thanks for the answers so far. I'm worried the solutions that parse the text may not work on non English operating systems. Does anybody know a way around this, or am I going to have to bite the bullet and write a console program to get this right.

like image 427
Scott Langham Avatar asked Dec 09 '08 15:12

Scott Langham


People also ask

How do I check if a service is running Windows command line?

Run the sc query command. The sc query command queries all Windows services and returns information such as the name ( SERVICE_NAME ), display name ( DISPLAY_NAME ), state, and more. If you don't know the service name, sc query is your friend to find it.

How can I see what services are running in cmd?

Open Start. Search for Command Prompt, right-click the top result, and select the Run as administrator option. (Optional) Type the following command to view a list of all the services and press Enter: sc queryex state=all type=service.

How do you check if a service is running?

You can do this by making your own Interface where you declare for example " isServiceRunning() ". You can then bind your Activity to your Service, run the method isServiceRunning(), the Service will check for itself if it is running or not and returns a boolean to your Activity.


2 Answers

sc query "ServiceName" | find "RUNNING" 
like image 195
Igal Serban Avatar answered Oct 03 '22 10:10

Igal Serban


Let's go back to the old school of batch programing on windows

net start | find "Service Name" 

This will work everywhere...

like image 21
Shahin Avatar answered Oct 03 '22 08:10

Shahin