Batch file to start an application once a service has started


  1. Posts : 3
    win7 64bit
       #1

    Batch file to start an application once a service has started


    Hi All

    New to the thread, but i am looking for some help.

    I want to create a batch file that will start on Windows start up, that will keep checking to see if a certain service has started then once the service has started it runs an application, then the batch file stops.

    The reason is that this application wont start until its service has started, but this service relies on database running and as such it can take some time to start up.

    If the application is tried before the service is running you get a message asking to try again, so i thought an automated start would be good.

    However i'm at a loss as to the correct syntax. Any help would be greatly appreciated.
      My Computer


  2. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #2

    Hey Foxman,

    Here is a very basic batch script that should get you well on your way.

    Code:
    @echo off
    rem note: program and service names are both case insensitive.
    set "service_to_wait_for=MyService"
    set "program_to_start=C:\MyProgram.exe"
    echo.&echo Waiting for service "%SERVICE_TO_WAIT_FOR%" to start...
    :loop
    sc query "%SERVICE_TO_WAIT_FOR%" | find "STATE" | find "RUNNING" || goto :loop
    cls& echo.& echo The service "%SERVICE_TO_WAIT_FOR%" is running.
    start "" "%PROGRAM_TO_START%"
    timeout 2
    Place the batch file in the Startup folder: C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup folder.
      My Computer


  3. Posts : 3
    win7 64bit
    Thread Starter
       #3

    Hi Pyprohly

    No wonder i could not figure this out. Many thanks for this. I will give it a go on Monday. Best Regards
      My Computer


  4. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #4

    Good luck. Do return should you require any help whatsoever. :)
      My Computer


  5. Posts : 3
    win7 64bit
    Thread Starter
       #5

    Hi Pyprohly

    Just wanted to let you know that script does exactly what i was after. Many thanks again, great help. Cheers
      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 11:53.
Find Us