title: Send email if task failed
body: <p>I'm writing a shell script that creates a log file of all the tasks it completed. At the very end of the script it creates a tar file and restarts a service. </p>
  
  <p>I would like the script to send an email if the tar process failed or if the service didn't start back up. I'm not sure how to check if the tar and service passed/failed.</p>
  
  <p>Here's an example of the shell script without checking if the tar or service completed...</p>
  
  <pre><code>#!/bin/bash
  
  # Shutdown service
  service $SERVICE stop
  
  # Task 1
  command &gt; some1.log
  
  # Task 2
  command &gt; some2.log
  
  # Task 3
  command &gt; some3.log
  
  # Compress Tar file
  tar -czf logfiles.tar.gz *.log
  
  # Start service
  service $SERVICE start
  
  # mail if failed
  mail -s "Task failed" | user@domain.com &lt;&lt; "the task failed"
  </code></pre>
  
  <p>Update: The script should not abort as I want the service to attempt to start again if any of the prior tasks did fail.</p>