Batch file to start an application once a service has started

Foxman50

New member
Local time
11:36 PM
Messages
3
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 My Computer

At a glance

win7 64bitintel6gbRadeon
Computer type
PC/Desktop
Computer Manufacturer/Model Number
custom
OS
win7 64bit
CPU
intel
Motherboard
Gigabyte
Memory
6gb
Graphics Card(s)
Radeon
Hard Drives
SSD
Antivirus
Avast
Browser
Mozilla
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 My Computer

At a glance

Windows 10, Windows 8.1 Pro, Windows 7 Profes...
Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
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 My Computer

At a glance

win7 64bitintel6gbRadeon
Computer type
PC/Desktop
Computer Manufacturer/Model Number
custom
OS
win7 64bit
CPU
intel
Motherboard
Gigabyte
Memory
6gb
Graphics Card(s)
Radeon
Hard Drives
SSD
Antivirus
Avast
Browser
Mozilla
Good luck. Do return should you require any help whatsoever. :)
 

My Computer My Computer

At a glance

Windows 10, Windows 8.1 Pro, Windows 7 Profes...
Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
Hi Pyprohly

Just wanted to let you know that script does exactly what i was after. Many thanks again, great help. Cheers
 

My Computer My Computer

At a glance

win7 64bitintel6gbRadeon
Computer type
PC/Desktop
Computer Manufacturer/Model Number
custom
OS
win7 64bit
CPU
intel
Motherboard
Gigabyte
Memory
6gb
Graphics Card(s)
Radeon
Hard Drives
SSD
Antivirus
Avast
Browser
Mozilla
Back
Top