Wednesday, October 8, 2014

Send an email when a Windows process completes

I was running a process that would take a couple of days to complete and wanted to be notified via email when it did finish. The following batch file did the trick. Note: You will need to download and extract the opensource blat mail tool ( http://sourceforge.net/projects/blat/ ). The script uses a stop file, so that once it completes and emails you, it won't continue to email you (if you have the check scheduled for every 5 minutes for example):


@ECHO OFF
set ERRORLEVEL=""


IF EXIST c:\scripts\script.stop (

    echo "Stop file exists, exiting..."
    exit

) ELSE (

    tasklist /FI "IMAGENAME eq your_process.exe" 2>NUL | find /I /N "your_process">NUL
    
    if "%ERRORLEVEL%"=="0" (
        echo Program is running
    ) ELSE (

        c:\scripts\blat.exe -server YOUR_EMAIL_SERVER -port MAIL_PORT -f FROM_EMAIL_ADDRESS -t YOUR_EMAIL_ADDDRESS -s "YOUR SUBJECT" -body "YOUR BODY OF TEXT"
        echo. 2>c:\scripts\script.stop

    )
)

No comments:

Post a Comment