Ecometry® Cron Jobs with Email Alerts

by Brennan Mann 24. April 2010 21:21

Having had system critical jobs running in the early hours of the morning, I thought it would nice to be able to wake up and have the job results waiting on my Smartphone. I created a HTML email application that could be easily placed in existing Batch/Cron jobs. The email application will send standard lists, dynamic attachments and dynamic reports. The application also includes a  customizable HTML template.  With just a few lines added to your existing Batch/Cron jobs, you are on your way.

Sample Screen Shot:

 

1.) Download the Email Job Notification Application from:

http://www.plasmawebservices.com/downloads

Run the setup.exe and extract the files. By default, the program will be installed into C:\program files\pws\emailjobnotification

The following files and folders will be created in <Install Directory>\EmailJobNotification Install:

  • EmailJobNotification.exe - Acutall email application
  • EmailJobNotification.exe.config - Application Config file ( contain SMTP settings )
  • test.bat - Test batch file for confirm SMTP connectivity 
  • favicon.ico - Application Icon
  • templates - Folder contain HTML and Image templates
  • templates\email.html - Email template
  • templates\jobPro_logo.gif - Email Image

You will need to configure the XML configuration file with your SMTP values. Edit the EmailJobNotification.exe.config configuration file in Notepad.

The file is located in the root of the installed directory. C:\program files\pws\emailjobnotification\EmailJobNotification.exe.config

        <EmailJobNotification.Properties.Settings>
            <setting name="smtpHost" serializeAs="String">
                <value>mail.myhost.com</value>
            </setting>
            <setting name="smtpUser" serializeAs="String">
                <value>smtpUser@test.com</value>
            </setting>
            <setting name="smtpPassword" serializeAs="String">
                <value>mypassword</value>
            </setting>
            <setting name="emailTo" serializeAs="String">
                <value>to_email@test.com</value>
            </setting>
            <setting name="emailFrom" serializeAs="String">
                <value>JobPro Support &lt;from_email@test.com&gt;</value>
            </setting>
            <setting name="emailSubject" serializeAs="String">
                <value>JobPro Notification</value>
            </setting>
            <setting name="emailCC" serializeAs="String">
                <value />
            </setting>
            <setting name="sTesting" serializeAs="String">
                <value>False</value>
            </setting>
            <setting name="sJobName" serializeAs="String">
                <value>My Job Name</value>
            </setting>
            <setting name="emailAttachment" serializeAs="String">
                <value />
            </setting>
        </EmailJobNotification.Properties.Settings>

The job name, to, cc and subject can be set in the batch file as environmental variables. If the environmental variables are present, the values in the EmailJobNotification.exe.config will be overridden.

2.) Create a test batch file to test your SMTP settings

Below is a sample test batch file. The quickest editor to use to create or modify batch files is Notepad. The sample below is included in the email application ( reference the "batch files" folder in the application folder ).

@echo off

: Set the Job Name to appear in the email
set JOBNAME=This is a test

: Path to your cron job output or log file
set LOGFILE=c:\new_log.txt

: Set email subject
set EMAIL_SUBJECT=JobPro Notification - This is a Test

: set email Recipients To list ( use ","to separate email addresses)  
set EMAIL_TO=test@test.com, test2@test.com

: set email Recipients CC list ( use ","to separate email addresses)
set EMAIL_CC=test3@test.com, test3@test.com

:Add Attachment 1
set ATTACHMENT1=c:\program_output.txt

:Add Attachment 2
set ATTACHMENT2=c:\program_report.txt

:Send Email
c:\pws\EmailJobNotification.exe

If you are not familiar with batch scripts, the ":" or "rem" can be used to make comments in the batch script.

3.) Add the new batch commands to your existing cron jobs. Below is an example of a production job:

@echo off

: *************************************************************
: *
: *  Who     Date            Comments
: *  -----   ---------       ---------------------------
: *  BM      09/01/09     Run order processing by division,
: *                                      send collates to printer and
: *                                      update PRINTJOB table
: *
: *************************************************************

: Division 01
echo Order Processing > E:\ECOMETRY\JOBPRO\logs\inspkjb1_log.txt
cronsched.exe E:\ECOMETRY\LIVECODE\Userstr2\inspkjb1 MGR 31000 E:\ECOMETRY\LISTENER\ECOMLIVE.ini >> E:\ECOMETRY\JOBPRO\logs\inspkjb1_log.txt
cscript /nologo E:\ECOMETRY\JOBPRO\VBS\orderprocessing.vbs 1 > E:\ECOMETRY\JOBPRO\logs\orderprocessing.txt

: Send Email Alert for division 01
set JOBNAME=Order Processing
set LOGFILE=E:\ECOMETRY\JOBPRO\logs\inspkjb1_log.txt
set EMAIL_SUBJECT=JobPro Notification - Order Processing Division 01
set EMAIL_TO=brennan@plasmawebservices.com
c:
cd c:\pws\email\
EmailJobNotification

: Division 02
cronsched.exe E:\ECOMETRY\LIVECODE\Userstr2\inspkjb2 MGR 31000 E:\ECOMETRY\LISTENER\ECOMLIVE.ini > E:\ECOMETRY\JOBPRO\logs\inspkjb2_log.txt
cscript /nologo E:\ECOMETRY\JOBPRO\VBS\orderprocessing.vbs 2 >> E:\ECOMETRY\JOBPRO\logs\orderprocessing.txt

: Send Email Alert for division 02
set JOBNAME=Order Processing
set LOGFILE=E:\ECOMETRY\JOBPRO\logs\inspkjb2_log.txt
set EMAIL_SUBJECT=JobPro Notification - Order Processing Division 02
set EMAIL_TO=brennan@plasmawebservices.com
c:
cd c:\pws\email\
EmailJobNotification

:End of Batch File

In the example above, we are "redirecting" the output from the cron job to a log file.  Basically instead of "seeing" the output from cronsched.exe in the command window, we are sending the output to a log file. The email application will parse the log file and find the job number and standlist. The standlist will be attached in the email notification.

For more information on MS-DOS redirection operators, please checkout the following URL:

http://www.febooti.com/products/command-line-email/batch-files/ms-dos-command-redirection.html

 

If you have any questions, please feel free to ask.

 

Next week, using ASP.Net 4.0 to create web portal for barometers and "drill-down" data reporting.

 

Tags: , , ,

Ecometry® | Programming

Comments

About the author

Brennan Mann started working on the Ecometry® platform in 2000. For the past ten years, Brennan has provided custom solutions and consultation for Ecometry® and other e-commerce platforms. In 2006, Brennan entered the realm of  Microsoft's .Net Technology.