我试图让我的系统在系统启动时向我发送一封电子邮件。使用以下命令可以按预期工作:
echo -e "TO:my-email-address@gmail.com \nSUBJECT:System Booted \n\nBeware I live! \n"$1 | 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:系统已启动\n\n当心我还活着!\n"
echo -e "TO:my-email-address@gmail.com\nSUBJECT:系统已启动\n\n当心我还活着!\n"
您可以使用这样的脚本:
#!/bin/sh
sendmail -t -vs <<EOF
TO:my-email-address@gmail.com
SUBJECT:System Booted
Beware I live!
$1
EOF
我是一名优秀的程序员,十分优秀!