Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Stopping services if they are started


  • Please log in to reply

#1
Computer Dr

Computer Dr

    New Member

  • Member
  • Pip
  • 8 posts
I'm Creating a batch file that stops services and starts them back.
Currently it blindly stop a list of services and Can blindly start them back.

I'm looking for the ability to stop the service ONLY if it is running
I got this peice of code from another thread, but it won't start themes back

sc query Themes| findstr /i running | if "%errorlevel%"=="0" (sc stop Themes) else (sc start Themes)
I'm not sure what I am missing. (not a programmer for one)
I don't think the errorlevel should be used in this instance.
Can someone lead me in the right direction? Any help is greatly appreciated.
  • 0

Advertisements


#2
SleepyDude

SleepyDude

    Trusted Helper

  • Malware Removal
  • 4,978 posts
Hi,

You need to split the commands, like this:
@echo off
sc query Themes| findstr /i running >nul
if "%errorlevel%"=="0" (sc stop Themes) else (sc start Themes)

The script will check if themes service is running and if it's true it will only stop the service, not sure if this is what you want.
If not running will be started.

Edited by SleepyDude, 04 September 2013 - 09:42 AM.

  • 0

#3
Computer Dr

Computer Dr

    New Member

  • Topic Starter
  • Member
  • Pip
  • 8 posts
I can do this two ways. have a single line for each service to check if it is on then turn it off and Vise versa

select 1 Stop services
select 2 Start services

If serivce is (%running%) then %startstop%

I don't want to stop a service that is not running, I get a message and it slows the file down.

Or similar with a few lines that get the services from a list of services. (not desired) I like putting breaks in the batch so I can tell which service is giving me issues.

Which do you think is better how can I do this. I guess I can have two lines for each service, but I was hoping for a simler way using the service code and not the errorcode
  • 0

#4
SleepyDude

SleepyDude

    Trusted Helper

  • Malware Removal
  • 4,978 posts
Hi,

You can do something like this:
@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

set SERVICES=themes power spooler
for %%S in (%SERVICES%) do (
   sc query %%S| findstr /i running >nul
   if "!errorlevel!"=="0" (sc stop %%S) else (sc start %%S)
)

  • 0

#5
Computer Dr

Computer Dr

    New Member

  • Topic Starter
  • Member
  • Pip
  • 8 posts
This is what I have so far. Sorry for the elementry coding, but it works great. I just need a text file with the services to
toggle on and off. I know there is a better way to do this though in less lines. I am using the state instead of errorlevel.

@echo off

================Input===============
:start
cls
color 0A
Echo select 1 for Stop Services
echo select 2 for Start Services

echo select q to quit
set /p userinp=Choose a number(1-6):
::can only go to 9

set userinp=%userinp:~0,1%

if "%userinp%"=="1" goto service
if "%userinp%"=="2" goto service

if /I "%userinp:~0,1%"=="q" goto exit
IF "%userinp%"=="" (ECHO Nothing is entered
GoTo :start)

=================Start of Code=============
:service
for /f "tokens=3" %%a in ('sc query "Themes"^|find "STATE"') do (if %%a==4 (goto running) else goto stopped)

:stopped
if "%userinp%"=="2" (
net start /y "Themes"
)
goto end

:running

if "%userinp%"=="1" (
net stop /y "Themes"
)
goto end



pause
:end
goto start
  • 0

#6
SleepyDude

SleepyDude

    Trusted Helper

  • Malware Removal
  • 4,978 posts
I that case use another for to process the text file and inside the for you already have.
  • 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