gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:38:04 25 4
gpt4 key购买 nike

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

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

关于linux - 在 rc.local 中通过管道将 echo 传送到 sendmail 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30882086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com