|Well.. it's taken me all day because I'm not a batch file writer.. but with the help of my good friend Google, I've managed to do it. I'll share the results in case anyone else ever has the question.. |
The batch file runs a local sql statement on a remote database, looking for a temp table that I am having created when I launch an .exe on my local box. If it finds that temp table.. program exits and all is well. If it doesn't see it, it shoots me an email via blat (I put my 3 blat files in C:\Windows). Most of you probably know what blat is, but if not.. it's a command line email program. Additionally, the server I was on didn't have smtp, so I installed QK SMTP server.. a little portable mail server I can setup just for this.
If you have any suggestions for my batch file, feel free to offer. I don't do much in this. In fact, I had EXIT in my code and couldn't figure out for the longest time that EXIT makes it retain the last %errorlevel% value, so my query was always returning a 0 (true). Putting END in seemed to resolve this for me. It was driving me mad for a good hour.
I then setup a Scheduled Task in Windows to run my batch file every 15 minutes all day, every day.
Anyway.. here's my code. Cheers!
osql -SJeffsServer -dmaster -Usa -Psecretpw -iC:\Jeff\BatchFiles\mysqlscript.sql -n -b > optionallogfilewithSQLresults.txt
IF ERRORLEVEL ==1 GOTO :NOTRUNNING
IF ERRORLEVEL ==0 GOTO :RUNNING
Echo %date% %time% not running at this time. >> C:\Jeff\BatchFiles\ProgramDownTimeLog.txt
c:\windows\blat.exe - -to "email@example.com, firstname.lastname@example.org" -f "This email is from..." -s "Subject of Email" -ti 30 -try 120 -noh -body "Your program quit running" -attach "C:\Jeff\BatchFiles\ProgramDownTimeLog.txt" -server mail.mydomain.com ---- (mail.mydomain.com was just my server name after installing my smtp program)