How to Configure Cronjob Email Alerts for Multiple Recipients Using MAILTO


3 views

The standard MAILTO directive in crontab only accepts a single email address as its parameter. When you need to notify multiple team members or systems about cronjob execution results, this becomes problematic.

Here are effective approaches to route cron output to multiple recipients:

1. Using Email Aliases (Recommended for System-Wide Solutions)

Create a distribution list on your mail server:

# /etc/aliases
cron-alerts: user1@example.com,user2@example.com,user3@example.com

Then in crontab:

MAILTO=cron-alerts
* * * * * /path/to/script.sh

2. Pipe Output to Mail Command

For more control, pipe the output to mailx:

0 * * * * /path/to/script.sh 2>&1 | mailx -s "Cron Report" \
user1@example.com,user2@example.com,user3@example.com

3. Use a Wrapper Script

Create a bash script that handles mailing:

#!/bin/bash
RECIPIENTS="user1@example.com user2@example.com user3@example.com"
OUTPUT=$(/path/to/script.sh 2>&1)
echo "$OUTPUT" | mailx -s "Cronjob Output" "$RECIPIENTS"

Then call this wrapper from crontab:

MAILTO="" # Disable default email
0 * * * * /path/to/wrapper.sh

Error Handling: Add error checking in wrapper scripts to prevent silent failures

Log Rotation: Consider logging to files when dealing with large outputs

Security: Validate recipient lists when using dynamic addresses

# Example with error handling
#!/bin/bash
RECIPIENTS="team@example.com"
LOG_FILE="/var/log/cronjobs/$(date +\%Y\%m\%d).log"

/path/to/script.sh > "$LOG_FILE" 2>&1
EXIT_CODE=$?

if [ $EXIT_CODE -ne 0 ]; then
    mailx -s "CRITICAL: Cronjob Failed" "$RECIPIENTS" < "$LOG_FILE"
fi

For modern infrastructures, consider:

  • AWS SNS topics with multiple subscribers
  • Slack/Teams webhook integrations
  • Custom monitoring solutions like Prometheus alerts

The MAILTO directive in crontab only accepts a single email address by default. While this works for basic notification needs, production environments often require alerts to be sent to multiple stakeholders - sysadmins, developers, and monitoring systems.

The simplest solution is to configure a distribution list on your mail server:

# In crontab
MAILTO=dev-team@yourcompany.com

# On mail server (Postfix example)
/dev/aliases:
dev-team: admin1@domain.com, admin2@domain.com, monitoring@domain.com

For systems without distribution list access, use mailx in the cron command itself:

0 * * * * /path/to/script.sh 2>&1 | mailx -s "Cron Output" \
  -a "From: cron@server.com" \
  user1@domain.com,user2@domain.com

Configure your MTA (Mail Transfer Agent) to handle cron outputs:

# Sendmail configuration example
MAILTO="| /usr/sbin/sendmail -t -i admin1@domain.com,admin2@domain.com"

# Exim4 configuration
MAILTO="| /usr/sbin/exim -i admin1@domain.com admin2@domain.com"

For cronjobs with verbose output, consider these approaches:

# Compress attachments
0 * * * * /path/to/script.sh | gzip | uuencode output.gz | \
  mailx -s "Compressed Cron Output" team@domain.com

# Send only on failure
0 * * * * /path/to/script.sh || echo "Job failed" | \
  mailx -s "ALERT: Cron Failure" oncall-team@domain.com

When implementing multiple recipients:

  • Use BCC for large recipient lists to avoid exposing emails
  • Consider rate limiting for high-frequency jobs
  • Implement proper mail server authentication