Scheduled Task that will conditionally launch a .bat file and then stop running

Hi,
Below novice level programmer, mostly cut and paste. I'm trying to create a scheduled task that will ping an IP every 5 minutes and launch a .bat file when the ping fails. I was able to copy something that works great, except I can't figure out how to get it to stop running once it launches the .bat file. I used the following as a .bat and just set it to run Daily, and then every 5 minutes under the advance tab. If someone can show me a better way to do this with my limited knowledge, I'd appreciate it. Again, ideally this would trigger the .bat file when the ping failed, and then stop the job from running so it would try to launch it again every 5 minutes that the server is down. Thanks much.

@echo off
for /f "delims=." %%I in ('ping -n 1 10.0.0.75') do if "%%I"=="Request timed out" (c:\Scenario2-Principal_SQL01_Failure.bat)
adamant40Asked:
Who is Participating?
 
tickettConnect With a Mentor Commented:
Interesting. It looks like deleting is easy but disabling is OS dependant. The easiest way seems to be to rename the job file. Add this to your Scenario2-Principal_SQL01_Failure.bat:

rename %systemroot%\tasks\myjob.job %systemroot%\tasks\myjob.old

Then change it back when you need it up and running again. I believe this will also work in server 2003:

schtasks /change disable /TN "myjob"

L
0
 
tickettCommented:
Try

@echo off
for /f "delims=." %%I in ('ping -n 1 10.0.0.75') do if "%%I"=="Request timed out" (call c:\Scenario2-Principal_SQL01_Failure.bat)
0
 
Jason Yousef, MSSr. BI  DeveloperCommented:
That's so simple, create a .bat file, attach it to a scheduled job to run it every 5 minutes:
 here's what to include in the bat file, change the IP to urs, and change 'myscript.bat' to your final .bat file :)
 
ping 0.0.0.0 | find /i "reply" > nul
if not errorlevel 1 goto :eof
myscript.bat
 
 
can you tell me what's the 2nd .bat file, maybe it could done in one step. which will be neat and cleaner approach
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
adamant40Author Commented:
Thank you for your answers,
Tickett, Unless I'm missing something you copied what I wrote. It does launch the correct script when the IP fails, but the problem is the next time that scheduled task runs it ALSO triggers because the IP is not reachable. I can't have it running the script every 5 minutes.
Huslayer: This maybe my ignorance in implementation, but your cooler script has the same problem. If I run it as a scheduled task it will attempt to launch the requested .bat file every 5 minutes.
What I'm trying to do is have a ping run every 5 minutes, and a .bat file launched when the ping fails, and then the job STOPS after running that .bat file ONE time. I'm triggering a SQL server failover, don't want to trigger it again 5 minutes later, and 5 minutes after that.  I saw a .vbs script that seemed to do this, but it was WAY over my head and someone would have to walk me through it bigtime. Plus it was more complicated than I needed. Really, just ping a server until it's down, run a script, stop.
0
 
tickettCommented:
So do you want the scheduled task disabled when the bat file is launch for failover?

You can use the AT command in your failover batch file: http://support.microsoft.com/kb/313565

My script was almost identical to yours but i added "call"- but now you have explained your problem differently- the solution will be different :)

L
0
 
adamant40Author Commented:
Tickett, sorry I missed the difference. I don't quite follow the script itself but I saw that it worked ;-) I'll try looking up the AT command, thank you for the link. I'm not always able to follow these things so if you feel like applying it to my exact situation as an example, I'd appreciate it.
0
 
SteveConnect With a Mentor Commented:
probably easiest to use the batchfile to work out whats going on.


try something like this:

@echo off

if not exist "C:\flagfile.txt" goto test
goto finish

:test
for /f "delims=." %%I in ('ping -n 1 10.0.0.75') do if "%%I"=="Request timed out" (c:\Scenario2-Principal_SQL01_Failure.bat)

:finish

In your 2nd batchfile just create a file called c:\flagfile.txt at the start.
If this file exists, the ping will be skipped and the bacthfile ends early.

Once you have solved the issue you can delete the flagfile manually or do it in the 2nd batchfile etc. once the file is missing, the ping testing will resume.
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
I fyou really want to disable the task the proper bit is to do schedule via SCHTasks.  however my Idea woudl have been to do similarly to "totallytonto" and create a file flag.

However I have a few questions:

1.  why not just make this function part of your existing script?
2.  What is your existing script doing?  -- I assume you are doing this because you don't want to pay the price required for SQL Enterprise edition so you can have an actual cluster, correct?
0
 
adamant40Author Commented:
Sorry. I am out sick and can't reply.
0
 
adamant40Author Commented:
Sorry. I am out sick and can't reply.
0
 
adamant40Author Commented:
Sorry for the delay. Thank you all for your replies and I am working my way through them to test and will report.

QCubed
1. The existing script is part of the manual failover process and works perfectly.
2. Running script causes the SQL mirror server to become the primary. Existing script changes a DNS entry to the 2nd server. Flushes DNS. Breaks the mirror. Etc. The need was indeed created because we can't afford to implement SQL Enterprise. But mostly this is the way I learn stuff. Take something I understand, Identify a tweek that can make it better, learn how to do the tweek and it can open up an entire new world for me.
0
 
SteveCommented:
Id keep it simple then and use a flagfile as shown in my example above.
0
 
adamant40Author Commented:
Thanks both. Tickett's solution worked instantly. Implementing totallyonto's suggestion will be a cooler way once I work it out.
0
 
adamant40Author Commented:
Hi all,
With help I got a great solution to this that anyone can implement, and learn from. Thought I'd paste it here in case it helps someone else.

@echo off
SET SERVER=10.0.0.75

echo "Checking %SERVER%..."
ping %SERVER% -n 1 | find "Reply from" > ping.log
if %errorlevel% == 1 echo "is down 1"
if %errorlevel% == 0 goto serverIsUp

ping %SERVER% -n 1 | find "Reply from" > ping.log
if %errorlevel% == 1 echo "is down 2"
if %errorlevel% == 0 goto serverIsUp

ping %SERVER% -n 1 | find "Reply from" > ping.log
if %errorlevel% == 1 echo "is down 3"
if %errorlevel% == 0 goto serverIsUp

ping %SERVER% -n 1 | find "Reply from" > ping.log
if %errorlevel% == 1 echo "is down 4"
if %errorlevel% == 0 goto serverIsUp
goto serverIsDown1


:ServerIsDown1
echo "Checking %SERVER% second time..."
ping %SERVER% -n 1 | find "Reply from" > ping.log
if %errorlevel% == 1 echo "is down 1"
if %errorlevel% == 0 goto serverIsUp

ping %SERVER% -n 1 | find "Reply from" > ping.log
if %errorlevel% == 1 echo "is down 2"
if %errorlevel% == 0 goto serverIsUp

ping %SERVER% -n 1 | find "Reply from" > ping.log
if %errorlevel% == 1 echo "is down 3"
if %errorlevel% == 0 goto serverIsUp

ping %SERVER% -n 1 | find "Reply from" > ping.log
if %errorlevel% == 1 echo "is down 4"
if %errorlevel% == 0 goto serverIsUp
goto serverIsDown2
goto exit



:ServerIsDown2
echo "Checking %SERVER% Final time..."
ping %SERVER% -n 1 | find "Reply from" > ping.log
if %errorlevel% == 1 echo "is down 1"
if %errorlevel% == 0 goto serverIsUp

ping %SERVER% -n 1 | find "Reply from" > ping.log
if %errorlevel% == 1 echo "is down 2"
if %errorlevel% == 0 goto serverIsUp

ping %SERVER% -n 1 | find "Reply from" > ping.log
if %errorlevel% == 1 echo "is down 3"
if %errorlevel% == 0 goto serverIsUp

ping %SERVER% -n 1 | find "Reply from" > ping.log
if %errorlevel% == 1 echo "is down 4"
if %errorlevel% == 0 goto serverIsUp
schtasks /Delete /TN "AR-PING" /F
Ping 10.0.0.1
echo "Sending EMAIL ALERT"
echo off
c:\AutoSQLFailover.bat




goto exit


:ServerIsUp
echo "Server %SERVER% is OK"
goto exit


:exit
echo "exiting"




0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.