Solved

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

Posted on 2010-08-21
14
803 Views
Last Modified: 2012-05-10
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)
0
Comment
Question by:adamant40
  • 7
  • 3
  • 2
  • +2
14 Comments
 
LVL 11

Expert Comment

by:tickett
ID: 33493637
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
 
LVL 21

Expert Comment

by:Jason Yousef, MS
ID: 33493720
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
 

Author Comment

by:adamant40
ID: 33497060
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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 11

Expert Comment

by:tickett
ID: 33497081
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
 

Author Comment

by:adamant40
ID: 33497146
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
 
LVL 11

Accepted Solution

by:
tickett earned 250 total points
ID: 33498490
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
 
LVL 27

Assisted Solution

by:Steve
Steve earned 250 total points
ID: 33501758
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
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33507897
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
 

Author Comment

by:adamant40
ID: 33508056
Sorry. I am out sick and can't reply.
0
 

Author Comment

by:adamant40
ID: 33508066
Sorry. I am out sick and can't reply.
0
 

Author Comment

by:adamant40
ID: 33554530
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
 
LVL 27

Expert Comment

by:Steve
ID: 33554767
Id keep it simple then and use a flagfile as shown in my example above.
0
 

Author Closing Comment

by:adamant40
ID: 33554875
Thanks both. Tickett's solution worked instantly. Implementing totallyonto's suggestion will be a cooler way once I work it out.
0
 

Author Comment

by:adamant40
ID: 33759011
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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question