I have my unit-status-mail.sh script and the unit-satus-mail@.service to send emails if any of interested services fails/restarts.
[unit-status-mail.sh]
#!/bin/bash
#MAILTO="root"
MAILTO="emailtowhom@sendingemail.com"
MAILFROM="my@mail.com"
#MAILFROM="unit-status-mailer"
UNIT=$1
EXTRA=""
for e in "${@:2}"; do
EXTRA+="$e"$'\n'
done
echo -e "printamos valor UNIT: $UNIT"
if [ $UNIT == "uwsgi" ]
then
ENCODED=`uuencode /path/to/attach/file.log file.log`
elif [ $UNIT == "mongod" ]
then
ENCODED=`uuencode /path/to/attach/file.log file.log`
fi
UNITSTATUS=$(systemctl status $UNIT)
sendmail $MAILTO <<EOF
$ENCODED
From:$MAILFROM
To:$MAILTO
Subject:Status mail for unit: $UNIT
Status report for unit: $UNIT
$EXTRA
$UNITSTATUS
LOG file attached
-----------
EOF
echo -e "Status mail sent to: $MAILTO for unit: $UNIT"
and here I have the [unit-satus-mail@.service]:
[Unit]
Description=Unit Status Mailer Service
After=network.target
[Service]
Type=simple
ExecStart=/bin/unit-status-mail.sh %I "Hostname: %H""Machine ID: %m""Boot ID: %b"
I'm trying to attach to the email the correspondig log file. If I call it manually
/bin/unit-status-mail.sh uwsgi "Hostname: %H""Machine ID: %m""Boot ID: %b"
it works well... email received with the attached log file. Instead when the service launches it automatically, I still receive the email, but NOT the log file.
I don't know what I'm missing, so I would be very thankful for any hints.