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

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,865 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,865 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,865 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