server-scripts/mail_wrap_script.sh

25 lines
520 B
Bash
Executable File

#!/bin/bash
SCRIPT=$1
shift
FILE_ID=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 32 | head -n 1)
touch /tmp/$FILE_ID.log
chmod a-rwx,u+r /tmp/$FILE_ID.log
$SCRIPT "$@" 2>&1 | tee /tmp/$FILE_ID.log
ERR=${PIPESTATUS[0]}
OUTPUT=$(cat /tmp/$FILE_ID.log)
rm /tmp/$FILE_ID.log
if [ $ERR -ne 0 ]; then
MAILTO=$(cat "$HOME/.admin_mail")
echo "Sending error mail"
echo -e "An error occured during the execution of $SCRIPT ($ERR).\n\n$OUTPUT" | mailx -s "$(hostname) $SCRIPT failure" "$MAILTO"
echo "Sent"
exit 1
fi