Bash 带有 sendmail 的脚本在手动执行而不是从 crontab 执行时发送电子邮件

Bash script with sendmail delivers email when executed manually but not from crontab

我编写了以下 bash 脚本,以便在我的网站出现问题时向我发送警报:

#!/bin/bash

# 1. download the page
BASE_URL="https://www.example.com/ja"

JS_URL="https://www.example.com/"

# # 2. search the page for the following URL: /sites/default/files/google_tag/google_tag.script.js?[FIVE-CHARACTER STRING WITH LETTERS AND NUMBERS]

curl -k -L ${BASE_URL} 2>/dev/null | grep -Eo "/sites/default/files/google_tag/google_tag.script.js?[^<]+" | while read line
do
    # 3. download the js file
    if curl -k -L ${JS_URL}/$line | grep gtm_preview >/dev/null 2>&1; then
       # 4. check if this js file has the text "gtm_preview" or not; if it does, send an email
       # echo "Error: gtm_preview found"
       sendmail error-ec2@example.com < email-gtm-live.txt
    else
      echo "No gtm_preview tag found."
    fi
done

我是从 Amazon EC2 Ubuntu 实例 运行 宁此。当我像 ./script.sh 一样手动执行脚本时,我在我的网络邮件收件箱中收到一封电子邮件 example.com

但是,当我通过 crontab 将此脚本配置为 运行 时,邮件不会通过 Internet 发送;相反,它被发送到 EC2 实例上的 /var/mail

我不明白为什么会这样或者我能做些什么来解决它。为什么 sendmail 在 bash 中的 运行 与 crontab 中的 运行 的行为不同?

请注意,crontab 执行的 PATH 环境变量不同于典型的交互式会话。此外,并非所有相同的环境变量都已设置。考虑指定 sendmail 可执行文件的完整路径(您可以通过发出 'which sendmail' 命令来了解)。