Sendmail不能与crontab (bash)一起工作



我创建了一个磁盘清理脚本,该脚本在清理后发送状态电子邮件。现在,当我通过命令行运行它时,它执行得很好,但通过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 

请告诉我解决方法,谢谢

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

/usr/sbin/sendmail "user@domain.com" < file.txt 

消息头和正文之间需要空行。

{
    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 

其他几点:

  • 这里不需要子shell,所以我将周围的括号改为大括号
  • 因为这是bash,所以$(cat file)有一个简写——$(< file)

在cron作业和CLI执行之间,环境变量可能不同。

您可以检查"env"从cron和从命令行执行的命令

由于selinux策略,我的sendmail无法在redhat上发送电子邮件

Jun 13 04:08:04 xxxx setroubleshoot[3103271]: SELinux is preventing /usr/sbin/postdrop from connectto access on the unix_stream_socket /var/spool/postfix/public/pickup. For complete SELinux messages run: sealert -l eeffd46d-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Jun 13 04:08:04 xxxx setroubleshoot[3103271]: SELinux is preventing /usr/sbin/postdrop from connectto access on the unix_stream_socket /var/spool/postfix/public/pickup.#012#012*****  Plugin catchall (100. confidence) suggests   **************************#012#012If you believe that postdrop should be allowed connectto access on the pickup unix_stream_socket by default.#012Then you should report this as a bug.#012You can generate a local policy module to allow this access.#012Do#012allow this access for now by executing:#012# ausearch -c 'postdrop' --raw | audit2allow -M my-postdrop#012# semodule -X 300 -i my-postdrop.pp#012

就像这里建议的那样:

    # grep postdrop /var/log/audit/audit.log | audit2allow -M mypol
    # semodule -i mypol.pp

最新更新