title: How to send email for failed jobs in shell scripts
body: <p>I have a shell script. To this script I am passing arguments from a file. This file contains tables names</p>
  
  <p>The script is working fine. I am able execute the command for all the tables in the file.</p>
  
  <p><code>shell script</code></p>
  
  <pre><code>#!/bin/bash
  
  [ $# -ne 1 ] &amp;&amp; { echo "Usage : $0 input file "; exit 1; }
  input_file=$1
  
  TIMESTAMP=`date "+%Y-%m-%d"`
  touch /home/$USER/logs/${TIMESTAMP}.success_log
  touch /home/$USER/logs/${TIMESTAMP}.fail_log 
  success_logs=/home/$USER/logs/${TIMESTAMP}.success_log
  failed_logs=/home/$USER/logs/${TIMESTAMP}.fail_log
  
  #Function to get the status of the job creation
  function log_status
  {
         status=$1
         message=$2
         if [ "$status" -ne 0 ]; then
                  echo "`date +\"%Y-%m-%d %H:%M:%S\"` [ERROR] $message [Status] $status : failed" | tee -a "${failed_logs}"
                  #echo "Please find the attached log file for more details"
                  #exit 1
                  else
                      echo "`date +\"%Y-%m-%d %H:%M:%S\"` [INFO] $message [Status] $status : success" | tee -a "${success_logs}"
                  fi
  }
  
  
  while read table ;do 
    sqoop job --exec $table &gt; /home/$USER/logging/"${table}_log" 2&gt;&amp;1
    g_STATUS=$?
    log_status $g_STATUS "Sqoop job ${table}"
    cp /home/$USER/logging/"${table}_log" /home/$USER/debug/`date "+%Y-%m-%d"`/logs/
  done &lt; ${input_file}
  </code></pre>
  
  <p>Now I want to send emails to my email address for <code>failed jobs</code>.</p>
  
  <p><code>Requirements</code></p>
  
  <pre><code>1) Send email for each failed job i.e If `status log` has failed job for one particular table then I want email sent out saying job for that table has failed. 
  
  2) Or Send out one email for all the jobs that have failed for one input file.
  </code></pre>
  
  <p>Which is the best method to approach. I would like the <code>2nd</code> option atleast it will reduce the no of emails to go through. </p>
  
  <p>But better if I know both methods to do</p>
  
  <blockquote>
    <p><code>edited function log_status</code></p>
  </blockquote>
  
  <pre><code>#Function to get the status of the job creation
  function log_status
  {
         status=$1
         message=$2
         if [ "$status" -ne 0 ]; then
                  echo "`date +\"%Y-%m-%d %H:%M:%S\"` [ERROR] $message [Status] $status : failed" | tee -a "${failed_logs}"
                  mail -a mail.txt -s "This is the failed job log" user@example.com &lt; /home/$USER/logs/${TIMESTAMP}.fail_log
                  #exit 1
                  else
                      echo "`date +\"%Y-%m-%d %H:%M:%S\"` [INFO] $message [Status] $status : success" | tee -a "${success_logs}"
                  fi
  }
  </code></pre>
  
  <p>If I do this will I get one email for all the failed jobs.</p>