How to Configure Cron Job Email Notifications for Failures and Output in Ubuntu


2 views

Cron has built-in email functionality that sends output from jobs to the owner of the crontab (or specified recipient). In Ubuntu, this relies on the system's mail transfer agent (MTA) being properly configured.

To set up email notifications for cron jobs, you need to either:

  1. Set the MAILTO variable in your crontab
  2. Configure the system-wide default recipient

Example crontab entry with MAILTO:

MAILTO="your.email@example.com"
* * * * * /path/to/your/script.sh

Cron automatically emails output when:

  • Script produces stdout/stderr output
  • Script exits with non-zero status

For better control, modify your scripts to:

#!/bin/bash
# Redirect stdout and stderr to log files
exec > /var/log/myscript.log 2>&1

# Your script logic here
if [ $? -ne 0 ]; then
  echo "Error occurred in script" | mail -s "Script Failed" your.email@example.com
fi

Since you're using SSMTP with Gmail:

  1. Ensure SSMTP is properly configured in /etc/ssmtp/ssmtp.conf
  2. Install mailutils package: sudo apt-get install mailutils
  3. Test email sending: echo "Test" | mail -s "Test Email" your.email@gmail.com

For scripts in /etc/cron.* directories, you can either:

# Option 1: Redirect output within script
#!/bin/bash
{
  # Your script commands
} 2>&1 | mail -s "Cron Job Output" your.email@example.com

# Option 2: Use logger to syslog
logger -t cron-script "Your message here"
  • Always include proper error handling in scripts
  • Consider using set -e to exit on error
  • Use log rotation for script output files
  • Test email notifications during non-critical times

Cron jobs in Ubuntu can automatically send email notifications through the system's mail transfer agent (MTA). When placing scripts in /etc/cron.{daily,hourly,monthly,weekly} directories, the cron daemon handles execution and can notify you of failures or script output.

Before configuring email alerts, ensure you have:

sudo apt-get install ssmtp mailutils
sudo service cron restart

Edit the SSMTP configuration file:

sudo nano /etc/ssmtp/ssmtp.conf

# Add these configurations:
root=your@gmail.com
mailhub=smtp.gmail.com:587
AuthUser=your@gmail.com
AuthPass=your_app_password
UseSTARTTLS=YES
FromLineOverride=YES

For scripts in cron directories, add proper error handling and email notification logic. Here's an example script template:

#!/bin/bash

# Redirect all output to a temporary file
LOG_FILE=/tmp/cron_$(date +\%Y\%m\%d_\%H\%M\%S).log
exec > >(tee -a "$LOG_FILE") 2>&1

# Your script commands here
echo "Starting backup process..."
rsync -avz /source/path /backup/path

# Check exit status and send email if failed
if [ $? -ne 0 ]; then
    echo "Backup failed with exit code $?" | mail -s "Cron Job Failed: Backup Script" your@email.com
    exit 1
fi

To set a default email recipient for all cron jobs, edit the crontab file:

sudo nano /etc/crontab

# Add or modify this line:
MAILTO="your@email.com"

For scripts that need to send output regardless of success/failure:

#!/bin/bash

# Main script logic
/path/to/your/script.sh

# Send complete output via email
cat <<EOF | mail -s "Cron Job Report: $(hostname)" your@email.com
$(hostname) - $(date)
----------------------------------------
$(/path/to/your/script.sh 2>&1)
----------------------------------------
EOF

If emails aren't being received:

  1. Check mail queue: mailq
  2. Test SSMTP configuration: echo "Test message" | mail -s "Test" your@email.com
  3. Verify cron logs: grep CRON /var/log/syslog