batch file to auto start a service

Dell LATITUDE D630
January 31, 2011 at 07:16:39
Specs: Windows 7, 2.201 GHz / 2045 MB
I want to put together a batch file that will auto start a service if it is found to not be running. Here is what I've put together thus far. It seems to work, but I want to see what those think who are smarter than I....
Here we go...
@echo off

:LOG
set batchlog=C:\batchlogs\servicewatch\batchlog.txt
set batchlog1=C:\batchlogs\servicewatch\status.txt
set templog=C:\batchlogs\servicewatch\templog.txt

date /t >>%batchlog%
time /t >>%batchlog%

:STATUS CHECK
Echo Checking status of Service... >>%batchlog%
sc query "bthserv" > %templog%
findstr /i "STATE" %templog% > %batchlog1%
Find /i "Stopped" %batchlog1%
echo %errorlevel%
pause
IF not errorlevel=1 goto NOTRUNNING
pause
IF errorlevel=1 goto RUNNING

:NOTRUNNING
cls
color 4f
echo Service was not running, will start now... >>%batchlog%
pause
SC START "bthserv"
goto END

:RUNNING
cls
Echo The service is running, will exit now. >>%batchlog%
pause
goto END


:END
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >>%batchlog%
del %templog%
timeout 5
del %batchlog1%
timeout 5
exit


the pauses are in there for when I was testing. To automate, they will be replaced with timeouts.


See More: batch file to auto start a service

Report •


#1
January 31, 2011 at 07:33:07
I use a similar script for something unrelated. I've modified it to fit your situation.
set batchlog=C:\batchlogs\servicewatch\batchlog.txt
net start bthserv >> %batchlog% 2>&1

How To Ask Questions The Smart Way


Report •
Related Solutions


Ask Question