Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Batch file to start a service if the service is not running


15 Jan 2011   #1

Windows 7 Ultimate x64
 
 
Batch file to start a service if the service is not running

Here what I want :

if servicename is running, do nothing.
if servicename is not running, then start servicename, then record the date and time.


based on my search [COLOR=blue ! important][COLOR=blue ! important]online[/COLOR][/COLOR], here is what I have so far:


net start | find "servicename" > nul 2>&1
if not .%errorlevel%.==.0. goto startservice


:startservice
net start "servicename"
echo service restarts @ %date% %time% >> C:\checklog.txt

pause




My problem is:
It looks like that the If statement is not working. Every time I run this batch file, it runs through all the command lines.
and I got a error message said the service is already running.
Please advice.


My System SpecsSystem Spec
.

15 Jan 2011   #2

Windows 8 Core X64
 
 

Why not just start it. If it's already running, nothing will happen. For instance, if I try to start BITS and it's already running, this is what I get:

net start "Background Intelligent Transfer Service"
The requested service has already been started.

More help is available by typing NET HELPMSG 2182.

If I run this from a batch file:

net start "Background Intelligent Transfer Service"
echo %errorlevel%


I get:

net start "Background Intelligent Transfer Service"
The requested service has already been started.

More help is available by typing NET HELPMSG 2182.

C:\Users\rrkurtz>echo 2
2

So if %errorlevel% == 2 then it's already running. So the batch file could look like this:

net start "Background Intelligent Transfer Service"

FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET date=%mm%%dd%%yyyy%
Rem ERRORLEVEL 1 checks all levels > 0
if ERRORLEVEL 1 echo "Started BITS" %date% > logfile.txt
My System SpecsSystem Spec
16 Jan 2011   #3

Windows 7 Ultimate x64
 
 

I don't not want to just run start service before we know it's not running, it may create another kind of issue.
anyone know what's wrong with my code or the if statement?
My System SpecsSystem Spec
.


16 Jan 2011   #4

Windows 8 Core X64
 
 

This works for me:

Rem This one is running
net start | find "Task Scheduler" > nul 2>&1
if %ERRORLEVEL%==0 echo "Task Scheduler is running"
if %ERRORLEVEL%==1 echo "Task Scheduler not running"

Rem This one is not running
net start | find "Task SchedulXr" > nul 2>&1
if %ERRORLEVEL%==0 echo "Task SchedulXr is running"
if %ERRORLEVEL%==1 echo "Task SchedulXr not running"

Note that if %ERRORLEVEL%==1 will be true for any value of 1 or greater.
My System SpecsSystem Spec
17 Jan 2011   #5

Linux Mint 17 Cinnamon | Win 7 Ult x64
 
 

Quote   Quote: Originally Posted by beidog View Post
Here what I want :

if servicename is running, do nothing.
if servicename is not running, then start servicename, then record the date and time.


based on my search [COLOR=blue ! important][COLOR=blue ! important]online[/COLOR][/COLOR], here is what I have so far:


net start | find "servicename" > nul 2>&1
if not .%errorlevel%.==.0. goto startservice

:startservice
net start "servicename"
echo service restarts @ %date% %time% >> C:\checklog.txt
Unless I'm missing something.. This will ALWAYS try to start the service. If the if not... test returns 0 the processing will drop to the :startservice section... Won't it?

Would this have worked?

net start | find "servicename" > nul 2>&1
if not .%errorlevel%.==.0. goto startservice
goto end

:startservice
net start "servicename"
echo service restarts @ %date% %time% >> C:\checklog.txt

:end
Pause
My System SpecsSystem Spec
20 Jul 2012   #6

Windows 7, 64 bit (on my 'main' computer)
 
 

I'm attempting to make a simple batch file one line, to stop the spooler service. I then try to verify through the GUI and hit refresh only to find that the although the cmd line says it stopped, it is still running accourind to the GUI.

I've tried;

elevate net stop spooler

as well as

elevate sc spooler stop

Niether will succeed.
My System SpecsSystem Spec
20 Jul 2012   #7

Windows 8 Core X64
 
 

What is elevate? It's not part of my Win 7 system.

How about creating the batch file without elevate, then right click on the batch file and select Run as Administrator? Then creat4e a link to it and when you run the link, the batch file should run with Administrator privileges.
My System SpecsSystem Spec
Reply

 Batch file to start a service if the service is not running




Thread Tools




Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 05:32 PM.
Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App
  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33