random-scripts/cronjob-warn-smtp-queue

31 lines
800 B
Bash
Executable file

#!/bin/bash
# Source the mail configuration.
source /etc/mail.conf "mail-queue" 2>/dev/null || {
printf "%s: %s\\n" "${0##*/}" "Failed to source /etc/mail.conf" >&2
exit 1
}
# Don't do anything unless 'mailq' is installed.
hash mailq 2>/dev/null && {
# Prevent a race with other cron jobs that produce emails.
sleep $(( RANDOM % 180 ))
# Get the queue
TMP_OUTPUT="$(mailq)"
[[ -z "$TMP_OUTPUT" ]] && exit 0
# Send the message.
if [[ -n "${EMAIL_TO[*]}" ]]; then
mailx "${MAILX_ARGS[@]}" -S "from=$EMAIL_FROM" -s "Mail queue" "${EMAIL_TO[@]}" <<<"$TMP_OUTPUT" 2>/dev/null || {
printf "%s: %s\\n" "${0##*/}" "mailx command failed" >&2
exit 1
}
else
printf "%s: %s\\n" "${0##*/}" "no recipient configured for mail delivery" >&2
exit 1
fi
}
exit 0