CRON的AWS SES sendmail失败

Jay*_*ard 2 bash cron amazon-web-services amazon-ses

通过 将Amazon SES与Sendmail集成在一起,我将SES配置为允许它从经过验证的电子邮件地址发送电子邮件。我能够使用已验证的电子邮件地址从命令行成功发送电子邮件:

sudo /usr/sbin/sendmail -f from@example.com to@example.com < file_to_send.txt
Run Code Online (Sandbox Code Playgroud)

接下来,我设置了一个bash脚本来收集一些每日报告信息。

#!/bin/bash                                                                                                                                                                                       

# copy the cw file                                                                                                                                                                       
cp /var/log/cwr.log /cwr_analysis/cwr.log                                                                                                          

# append the cw info to the subject file                                                                                                                                                 
cat /cwr_analysis/subject.txt /cwr_analysis/cwr.log  > /cwr_analysis/daily.txt                                                                                                    

# send the mail                                                                                                                                                                                   
/usr/sbin/sendmail -f from@example.com to@example.com < /cwr_analysis/daily.txt   
Run Code Online (Sandbox Code Playgroud)

如果我从命令行手动运行bash脚本,则会收集报告并通过电子邮件将其发送为应有的状态。我更改了文件权限,以允许它由根用户执行(类似于AWS实例上的其他CRON作业):

-rwxr-xr-x 1 root     root       375 Jan  6 17:37 cwr_email.sh
Run Code Online (Sandbox Code Playgroud)

问题

我设置了一个CRON作业,并将其设置为每5分钟运行一次以进行测试(该脚本旨在在生产开始后每天运行一次):

*/5 * * * * /home/ec2-user/cwr_email.sh
Run Code Online (Sandbox Code Playgroud)

bash脚本将复制并正确地附加daily.txt文件,但不会发送电子邮件。电子邮件假脱机中没有跳动或任何其他错误。

今天,我花了大部分时间寻找答案,并且许多搜索最终都陷入了死胡同,几乎没有使用CRON通过AWS SES发送电子邮件的信息。

如何解决此问题?

Kar*_*hah 5

cron的一个“问题”是缺少环境变量(出于明显的安全原因)。您可能缺少PATH和HOME。您可以直接在脚本中或在crontab文件中定义它们。

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/us??r/bin在调用sendmail脚本之前将其添加到crontab中,它应该可以正常工作

#!/bin/bash  
#Adding the path                                                                                                                                                                                     
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/us??r/bin

# copy the cw file                                                                                                                                                                       
cp /var/log/cwr.log /cwr_analysis/cwr.log                                                                                                          

# append the cw info to the subject file                                                                                                                                                 
cat /cwr_analysis/subject.txt /cwr_analysis/cwr.log  > /cwr_analysis/daily.txt                                                                                                    

# send the mail                                                                                                                                                                                   
/usr/sbin/sendmail -f from@example.com to@example.com < /cwr_analysis/daily.txt 
Run Code Online (Sandbox Code Playgroud)

您必须进行测试,直到按照脚本的要求定义了所有必需的变量。