stop / start windows service using findstr -
i need stop , start service when parameters met in .txt file.
when app can't connect database in our test environment (regular occurence) generates following file type in server folder (file date changes each occurence)
selftest-20141126181000-red.txt
within file phrase
red: server failed start: unable open database: unable connect database
every time generated, along phrase need stop , start specific service.
i have never used findstr command think useful here. textfile needs wildcarding obviously, , results file generating data , time service stopped , started. following work?
findstr "red: server failed start: unable open database: unable connect database” *red.txt if %errorlevel%==0 ( sc stop "my service" sc start "my service" echo %date% %time% database >> results.txt )
plan have running through scheduled tasks every 30mins or so
well, once have 1 such file, script continue find , restart service every 30 minutes. you'd need delete or move red file.
the second problem sc start
command may fail while service still being stopped - sc stop
returns request delivered, , not wait. you'd need repeated sc query "servicename" | find "running"
check. , while service still running, give more time running ping -n 2 127.0.0.1 > nul
. busy-waiting isn't nice, there's no clean sleep
command ping
common hack that.
Comments
Post a Comment