linux - 在带有 cron 的 Linux 脚本中使用 ssmtp

标签 linux cron

我创建了以下 Linux 脚本:

#!/bin/sh

tempfront=27
tempreartop=60
temprearbottom=65


if [ "$tempfront" -gt 26 ] || [ "$tempreartop" -gt 70 ] || [ "$temprearbottom" -gt 70 ]; then

echo "To: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="513c28343c30383d113c30383d7f323e3c" rel="noreferrer noopener nofollow">[email protected]</a>
From: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="137e6a7c677b7661767e727a7f53747e727a7f3d707c7e" rel="noreferrer noopener nofollow">[email protected]</a>
Subject: Test Alert

ALERT
Front temp: $tempfront oC
Back temp up: $tempreartop oC
Back temp down: $temprearbottom oC" > /home/odroid/alerts.txt

ssmtp <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9df0e4f8f0fcf4f1ddf0fcf4f1b3fef2f0" rel="noreferrer noopener nofollow">[email protected]</a> < /home/odroid/alerts.txt

fi

我手动执行此脚本并且工作正常,它将电子邮件警报发送到 [email protected] 。以下是 Gmail 发送的内容:

    Return-Path: <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a7cadec8d3cfc2d5c2cac6cecbe7c0cac6cecb89c4c8ca" rel="noreferrer noopener nofollow">[email protected]</a>>
    Received: from <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e3327312a363b2c3b333f37321e39333f3732703d3133" rel="noreferrer noopener nofollow">[email protected]</a>
 (ppp046177171064.abcs.fe. [xx.xx.xx.xx])
            by mx.google.com with ESMTPSA id k2sm8590878wix.4.2015.04.26.12.54.57
            for <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3c514559515d55507c515d5550125f5351" rel="noreferrer noopener nofollow">[email protected]</a>>
            (version=TLSv1 cipher=RC4-SHA bits=128/128);
            Sun, 26 Apr 2015 12:54:59 -0700 (PDT)
    Message-ID: <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2743171342674a5f09404848404b420944484a" rel="noreferrer noopener nofollow">[email protected]</a>>
    Received: by <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3a5743554e525f485f575b53567a5d575b535614595557" rel="noreferrer noopener nofollow">[email protected]</a> (sSMTP sendmail emulation); Sun, 26 Apr 2015 22:54:56 +0300
    Date: Sun, 26 Apr 2015 22:54:56 +0300
    To: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5835213d353931341835393134763b3735" rel="noreferrer noopener nofollow">[email protected]</a>
    From: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="214c584e55494453444c40484d61464c40484d0f424e4c" rel="noreferrer noopener nofollow">[email protected]</a>
    Subject: Test Alert

    ALERT
    Front temp: 27 oC
    Back temp up: 60 oC
    Back temp down: 65 oC

但是当我将其设置为在 cron 作业中运行时,电子邮件会像这样损坏:

.....
From: root <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="630e1a0c170b0611060e020a0f23040e020a0f4d000c0e" rel="noreferrer noopener nofollow">[email protected]</a>>
X-Google-Original-From: root (Cron Daemon)
Received: by <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2f4256405b474a5d4a424e46436f48424e4643014c4042" rel="noreferrer noopener nofollow">[email protected]</a> (sSMTP sendmail emulation); Sun, 26 Apr 2015 23:00:01 +0300
Date: Sun, 26 Apr 2015 23:00:01 +0300
To: root
Subject: Cron <root@odroid> /home/odroid/testalert.sh >/dev/null
Content-Type: text/plain; charset=ANSI_X3.4-1968
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>

/home/odroid/testalert.sh: 19: /home/odroid/testalert.sh: ssmtp: not found

而且 [email protected] 也没有收到它。 有人可以解释一下吗?为什么 cron 会把事情搞砸?

更新: 我注意到其他一些 cron 作业也输出到电子邮件!

...
Date: Mon, 27 Apr 2015 06:30:01 +0300
From: Cron Daemon <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="721f0b1d061a1700171f131b1e32151f131b1e5c111d1f" rel="noreferrer noopener nofollow">[email protected]</a>>
To: root
Subject: Cron <root@odroid> /home/odroid/motion_day.sh >/dev/null
Content-Type: text/plain; charset=ANSI_X3.4-1968
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>

[0] Processing thread 0 - config file /etc/motion/motion.conf
[0] Motion 3.2.12 Started
[0] Motion going to daemon mode

这只是一个网络摄像头监控脚本,与电子邮件无关! 其 cron 条目是

30 6 * * * /home/odroid/motion_day.sh >/dev/null

最佳答案

Cron 不会弄乱这里的任何东西。默认情况下,当安装本地 MTA 时,cron 会将作业的输出(stderr 和 stdout)邮寄给拥有该作业的用户,这就是此处发生的情况。

您执行的脚本失败,因为在 PATH 中找不到 ssmtp 程序(如 header 所示,该程序指向 /usr/bin:/bin )。

只要在调用ssmtp时使用完整的绝对路径,就可以了。

编辑:在您的 crontab 中,您将 stdout 重定向到/dev/null,但是如果脚本在 stderr 上生成输出,那么仍然会使用该输出发送一条消息。通常这是合理的,因为许多标准 UNIX 工具在有重要信息需要报告(例如错误)之前都保持沉默,在这种情况下您希望收到通知。其他工具有一个“安静”标志或类似的标志来减少其冗长,可以在这种情况下使用。

如果您根本不想发送任何消息,您可以重定向 stdout 和 stderr:两者:

... >/dev/null 2>&1

这样,即使出现问题,cron 也不会发送邮件,因此您应该仅在确保工作正常运行后才执行此操作。

关于linux - 在带有 cron 的 Linux 脚本中使用 ssmtp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29882937/

相关文章:

python - 如何确保脚本始终使用cron运行?

linux - 无效谓词 zcat

c++ - Red Hat 7下如何使用log4cxx

c++ - Linux 上运行程序的目录?

linux - 如何从特定时间开始无限地每 1 小时执行一次 cron 作业?

ruby-on-rails - 命令适用于 bash 但不适用于 cron

java - 简单的 Java 命令不在 Cron 中运行

c - FUSE lib passthrough.c 示例。它在哪里反射(reflect)了我的/确切的?

Java:在 Linux 中使用包管理器中的 jar 库

linux - 如何每天每十二小时运行一次命令?试过这不起作用 :/12 * * * * * mycommand