Click here to Skip to main content
15,900,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've created a batch file that changes the startup type of a service to automatic (delayed start) and now have attached a script which checks that the service is running and if not starts it, however when testing they don't seem to work at all.

source of service restart portion of my script: windows - How to check if a service is running via batch file and start it, if it is not running? - Stack Overflow[^]

My Script goes as follows:
@ECHO OFF
sc \\127.0.0.1\ config "<Service Here>" start= delayed-auto
SET SvcName="<Service Here>"
:A
rem monitering of the service
sc queryex "%SvcName%" | FIND "STATE" | FIND /v "RUNNING" > NUL && (
    echo %SvcName% is not running 
    echo start %SvcName%

    net start "%SvcName%" > NUL || (
        echo "%SvcName%" wont start 
	echo %date% %time% : %SvcName% is not running >> log.txt
	GOTO:B
    )
    echo "%SvcName%" is started
    GOTO:B
) || (
    echo "%SvcName%" is running
    echo %date% %time% : %SvcName% is running >> log.txt
    :B
    timeout /t 5
    rem this is the timer which sets how frequently the service is checked 1800 for an hour
    GOTO :A
)


What I have tried:

I've created a script that runs the two scripts mentioned above one after the other however that doesn't work either.

I just don't understand why they work separately but not together.(Anyone know how to resolve this).
Posted
Updated 12-Feb-19 22:26pm
Comments
Graeme Cooper 12-Feb-19 5:00am    
I do think this maybe an Admin issue however I'm not sure.
Richard MacCutchan 12-Feb-19 5:08am    
Have you tried running it under Admin privileges? What are the other scripts you mention? Do these commands work when you run them direct from a command prompt?
Graeme Cooper 12-Feb-19 10:10am    
To simplify everything I've said up to this point. The main issue is that once the services' startup type has been changed the script refuses to go through the loop and check the services' status without aid of the user.
Graeme Cooper 12-Feb-19 11:17am    
The combination of the two scripts is shown above--- "My Script goes as follows"
Graeme Cooper 12-Feb-19 11:20am    
I've tested both parts of the script and only today find that they don't work consecutively in a singular batch file.

The DOS timeout command is often problematic, I would advise to use a PowerShell script, this also allows you to wait for a process to finish.
# Wait for all processes starting with Unins to finish
# Time out after 1 minute
Write-Host "Waiting ..."
wait-process -name Unins* -Timeout 60
Read-Host -Prompt "Done, press Enter to exit"
 
Share this answer
 
Remove quotation marks at the SET SvcName part of the script.
Mission accomplished.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900