为什么 cron 没有成功 运行 我的 bash 脚本

Why doesn't cron successfully run my bash script

我在 root 用户的 crontab 中列出了一个 cron 作业:

*/5 * * * * /usr/local/bin/service_monitor.sh

文件内容如下:

#!/usr/bin/bash -e


/usr/bin/systemctl is-active --quiet nginx && aws cloudwatch put-metric-data --metric-name hearbeat-nginx --namespace heartbeat --value 1 --region eu-west-1
/usr/bin/systemctl is-active --quiet prosody && aws cloudwatch put-metric-data --metric-name heartbeat-prosody --namespace hearbeat --value 1 --region eu-west-1

该文件是可执行的,当我 运行 它直接作为 root 时,它 运行 没有问题,我得到了 AWS CloudWatch 中记录的指标,所以我知道 AWS 没有问题cli 或权限。但是,当我尝试 运行 它作为 cron 作业时,我没有看到创建任何指标。我可以在系统日志中看到该文件正在由 cron 执行。有什么想法吗?

aws 可能不会在 PATH 上传递给此程序的环境。作为最佳做法,请考虑

  • 总是在 cron 配置文件中通过绝对路径引用可执行文件(易于测试)或
  • 始终明确设置 PATH(更具可读性)。

此外,您打字有误 hearbeat