Search code examples
linuxshellemailsendmailboot

Piping echo into sendmail in rc.local fails


I'm trying to get my system to send me an email when the system boots. Using the following command works as intended:

echo -e "TO:my-email-address@gmail.com \nSUBJECT:System Booted \n\nBeware I live! \n"$1 | sendmail -t -vs

But if I add it to rc.local like so:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

#----- Print the IP address
#_IP=$(hostname -I) || true
#if [ "$_IP" ]; then
#  printf "My IP address is %s\n" "$_IP"
#fi

#----- Notify on boot
/home/alex/notify/boot.sh "`date`"

exit 0

I get:

sendmail: No recipients specified although -t option used

Why is this failing?


Solution

  • Problem in using bash/zsh vs /bin/sh

    Bash and Zsh have builtin echo with option -e, but a system /bin/echo - not. Compare:

    • /bin/echo -e "TO:my-email-address@gmail.com \nSUBJECT:System Booted \n\nBeware I live! \n"
    • echo -e "TO:my-email-address@gmail.com \nSUBJECT:System Booted \n\nBeware I live! \n"

    You may use script like this:

    #!/bin/sh
    sendmail -t -vs <<EOF
    TO:my-email-address@gmail.com
    SUBJECT:System Booted
    
    Beware I live!
    $1
    EOF