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


  1. Posts : 25
    Windows 7 Ultimate x64
       #1

    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 Computer


  2. Posts : 6,285
    Windows 10 Pro X64
       #2

    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
    Last edited by Ztruker; 15 Jan 2011 at 20:12.
      My Computer


  3. Posts : 25
    Windows 7 Ultimate x64
    Thread Starter
       #3

    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 Computer


  4. Posts : 6,285
    Windows 10 Pro X64
       #4

    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 Computer


  5. Posts : 784
    Linux Mint 17 Cinnamon | Win 7 Ult x64
       #5

    beidog said:
    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 Computer


  6. Posts : 3
    Windows 7, 64 bit (on my 'main' computer)
       #6

    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 Computer


  7. Posts : 6,285
    Windows 10 Pro X64
       #7

    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 Computer


 

  Related Discussions
Our Sites
Site Links
About 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 04:12.
Find Us