Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Start and Stop Services in a single batch file?


  • Please log in to reply

#1
AliL

AliL

    Member

  • Member
  • PipPip
  • 19 posts
Hi all, I was wondering if it is possible to start or stop a service using a single batch file with an "if" type structure?

I'm pretty new to this whole batch thing and was wondering if you could help me out.

I have been given this bit of code, but it seems a bit ugly to me because it requires part of the code to fail (i think) to trigger the other bit of code.

NET START "service" || NET STOP "service"

Personally I would have thought the code should look somewhat like this:

IF "service"==stopped "NET START 'service'"

IF "service"==started "NET STOP 'service'"

I expect many of you are screaming at your monitors that I have totally killed the code or missed the concept or something, but that's why I am here - so the experts can help.

I hope you can, and thanks in advance,

AliL
  • 0

Advertisements


#2
Ax238

Ax238

    Tech Staff

  • Technician
  • 1,323 posts
AliL,

"||" is a command line redirect that is designed to run the second command if the first fails. I personally feel it is elegant, rather than "ugly". It provides for very concise commands.

Nonetheless, as goes the saying, there's more than one way to skin a cat. Here's maybe what you would call a "prettier" version:
sc query service | findstr /i running | if "%errorlevel%"=="0" (sc stop service) else (sc start service)

I personally think your original is easier to comprehend. Notice that when %errorlevel% is not 0, it doesn't mean the command failed, it just means that the search string wasn't found in the output of the command.
  • 0

#3
AliL

AliL

    Member

  • Topic Starter
  • Member
  • PipPip
  • 19 posts
Whoa, that code looks crazy.

If the first one works fine then I think I'll stick with that.

It's just that it originally bugged me because the syntax is fine, it's just that (I'm no developer so I wouldn't know really - this is purely speculation from an outsider's point of view) if you were to run this through a debugger it might go mad because the first command is trying to bring a result, but it's already happening so it wouldn't know if it had caused it or not, as the output would still be "the service is running, woohoo that's what we wanted" which might cause the second bit of code not to run.

Did I make any sense there? Am I just being a paranoid a**e? :)


Thanks for the help anyway,

AliL
  • 0

#4
Ax238

Ax238

    Tech Staff

  • Technician
  • 1,323 posts
In batch programming, the error (%errorlevel%) is actually used quite frequently in performing decision logic. You can use errors this way in other languages as well, but batch programming makes it easier. You wouldn't get into any trouble with a debugger, the easiest debugger is the language itself!

Ax
  • 0

#5
AliL

AliL

    Member

  • Topic Starter
  • Member
  • PipPip
  • 19 posts
Ah cool, that's a nice bit of info.

Thanks for your time and the info, helped me de-confuse myself major time!

AliL
  • 0

#6
Ax238

Ax238

    Tech Staff

  • Technician
  • 1,323 posts
:) Glad to help!
  • 0

#7
Rick99

Rick99

    New Member

  • Member
  • Pip
  • 1 posts
Hi all,

can anyone please help how to write a batch file to stop services before my server stops at certian time and start after the server is restarted.

Thanks
  • 0

#8
skyhintack

skyhintack

    Member

  • Member
  • PipPipPip
  • 186 posts
Hi Rick99,

It would probably be best if you started your own topic with this question so you'll receive a faster response.

Thanks,
Sky
  • 0

#9
Computer Dr

Computer Dr

    New Member

  • Member
  • Pip
  • 8 posts
AX238, I've been playing with your line of code, and I have not been able to get it to work correctly. It will shut the service off(using themes)but will not start it back. errorlevel defaults at 0, afterwards the errorlevel displays 1067
Should the "running" be "state"?
I have a batch that stop a list of services IF they are running. I can blindly to sc stop service, but get errors if they are not running or non existant. I would like to see if they are running and if so, stop them.
Can you test this line of code and see if it works for you? I'm on windows 7 and running the batch as administrator.

Chris


AliL,

"||" is a command line redirect that is designed to run the second command if the first fails. I personally feel it is elegant, rather than "ugly". It provides for very concise commands.

Nonetheless, as goes the saying, there's more than one way to skin a cat. Here's maybe what you would call a "prettier" version:

sc query service | findstr /i running | if "%errorlevel%"=="0" (sc stop service) else (sc start service)

I personally think your original is easier to comprehend. Notice that when %errorlevel% is not 0, it doesn't mean the command failed, it just means that the search string wasn't found in the output of the command.


Edited by Computer Dr, 25 July 2013 - 02:41 PM.

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP