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
beidog

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
Ztruker

Windows 8.1 Pro 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
beidog

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
Ztruker

Windows 8.1 Pro 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
TanyaC

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
Jayce1983

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
Ztruker

Windows 8.1 Pro 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





Similar help and support threads
Thread Forum
Windows Update Service is not running, also won't start
Evening, I have been dealing with this issue for about a week. I had Windows Update set to notify me when updates are available so I can choose whether or not to install them. The window takes quite a while to open, and when it does it has the red shield with white "X" in it, and says "Check for...
Windows Updates & Activation
Windows Defender Service running when it is not running!
I found that the Windows Defender Service was running, even though I have other AV/Malware software that I thought turned it off. Checking in security is show both softwares with Defender shown as OFF! Is it safe to stop and disable that service? Glenn
System Security
!SASCore service running all the time, not running Superantispyware?
Found the the above service was running all the time and I only occasionally use SuperSpyware AntiSpyware as a standalone scan. This is solely as backup check to my Antimalwarebytes Pro. May I stop and disable it, and how would I remove it entirely? Thanks Glenn
General Discussion
Unable to start .jar file as a window service
Hello Everyone, This is my 1st thread, so i'm naive about the categories under which the issue i'm facing falls. I've a jar file which contains a very simple program of displaying "Hello" message in the console.I've pasted the code at the end of this thread.I want to run this jar file as a...
Software
My Laptop Slow Start up, Crash after sleep, Audio Service not running!
Toshiba Satellite Window 7 Ok. so the past couple of weeks my laptop has been running incredibly bad.. First Problem arise when I put my laptop to sleep. It was working when it was brand new, no problems whatsoever. But lately every time i would put my laptop to sleep, it would crash after been...
BSOD Help and Support
Clickable service start/stop cmd or bat file. Possible?
Not sure if I labelled this post right, or if I am even posting this in the right category/forum. If admins could direct this if need be, that would be cool. What I would like to do is have some kind of .cmd or .bat file that will turn on or restart my PS3 Media Server service, just by...
Performance & Maintenance

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 18:28.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App