Sendmail 不适用于 crontab (bash)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23736943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:29:03  来源:igfitidea点击:

Sendmail is not working with crontab (bash)

bashsendmailcrontab

提问by user2611539

I have created one disk cleanup script which after cleanup sends a status email. now when I run this through command line, it executes perfectly but through cronjob its not able to send staus mail rest the script is working fine though. I have read many solutions in google but nothing is working for me. I am using Bash on my Ubuntu machine. here is sendmail part of my script.

我创建了一个磁盘清理脚本,清理后会发送一封状态电子邮件。现在,当我通过命令行运行它时,它可以完美执行,但是通过 cronjob 它无法发送状态邮件,但脚本运行良好。我已经在谷歌阅读了很多解决方案,但没有什么对我有用。我在我的 Ubuntu 机器上使用 Bash。这是我的脚本的 sendmail 部分。

export CONTENT="/root/cleanup/cleanup.htm"                                           
export SUBJECT="Disk Space Clean Up Process : Completed @ $date_time"

(echo "Subject: $SUBJECT"
echo "`cat sendmail_list.txt`"
echo "MIME-Version: 1.0"
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat $CONTENT
)|/usr/sbin/sendmail -t 

please help me know the solution...thanks

请帮我知道解决方案...谢谢

回答by Lenix

Please add /usr/sbinbefore sendmail in sh file:

/usr/sbin在sh文件sendmail前添加:

/usr/sbin/sendmail "[email protected]" < file.txt 

Hope it helps https://stackoverflow.com/editing-help

希望它有帮助https://stackoverflow.com/editing-help

回答by glenn Hymanman

You need a blank line between the message header and the body.

消息标题和正文之间需要一个空行。

{
    echo "Subject: $SUBJECT"
    echo "$(< sendmail_list.txt)"
    echo "MIME-Version: 1.0"
    echo "Content-Type: text/html"
    echo "Content-Disposition: inline"
    echo ""
    cat $CONTENT
} | /usr/sbin/sendmail -t 

A couple of other things:

其他几件事:

  • you don't need a subshell here, so I changed the surrounding parentheses to braces
  • since this is bash, there's a shorthand for $(cat file)-- $(< file)
  • 这里不需要子shell,所以我将周围的括号更改为大括号
  • 因为这是 bash,所以有一个简写$(cat file)——$(< file)