在 rc.local 中通过管道将 echo 发送到 sendmail 失败

Piping echo into sendmail in rc.local fails

我正在尝试让我的系统在系统启动时向我发送一封电子邮件。使用以下命令按预期工作:

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

但是如果我像这样将它添加到 rc.local:

#!/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

我得到:

sendmail: No recipients specified although -t option used

为什么会失败?

使用 bash/zsh 与 /bin/sh

的问题

Bash 和 Zsh 有带选项 -e 的内置 echo,但系统 /bin/echo - 没有。 比较:

  • /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"

您可以使用这样的脚本:

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

Beware I live!

EOF