简体   繁体   中英

Using ssmtp in linux script with cron

I have created the following linux script:

#!/bin/sh

tempfront=27
tempreartop=60
temprearbottom=65


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

echo "To: myemail@mail.com
From: myotheremail@gmail.com
Subject: Test Alert

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

ssmtp myemail@mail.com < /home/odroid/alerts.txt

fi

I execute this script manually and works fine, it sends the email alert to myemail@mail.com. Here's what gmail sends out:

    Return-Path: <myotheremail@gmail.com>
    Received: from myotheremail@gmail.com
 (ppp046177171064.abcs.fe. [xx.xx.xx.xx])
            by mx.google.com with ESMTPSA id k2sm8590878wix.4.2015.04.26.12.54.57
            for <myemail@mail.com>
            (version=TLSv1 cipher=RC4-SHA bits=128/128);
            Sun, 26 Apr 2015 12:54:59 -0700 (PDT)
    Message-ID: <d04e@mx.google.com>
    Received: by myotheremail@gmail.com (sSMTP sendmail emulation); Sun, 26 Apr 2015 22:54:56 +0300
    Date: Sun, 26 Apr 2015 22:54:56 +0300
    To: myemail@mail.com
    From: myotheremail@gmail.com
    Subject: Test Alert

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

But when I set it to run in a cron job, the email comes corrupted like this:

.....
From: root <myotheremail@gmail.com>
X-Google-Original-From: root (Cron Daemon)
Received: by myotheremail@gmail.com (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

And also it is not being received by myemail@mail.com. Can someone explain this? Why cron is messing things up?

Update: I noticed that some other cron jobs are output to email as well!

...
Date: Mon, 27 Apr 2015 06:30:01 +0300
From: Cron Daemon <myotheremail@gmail.com>
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

This is just a script for webcam monitoring, nothing to do with email! The cron entry for that is

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

Cron doesn't mess up anything here. By default when a local MTA is installed, cron mails the output of a job (stderr and stdout) to the user owning the job, and that's what happened here.

Your executed script failed, because the ssmtp program couldn't be found in the PATH (which as shown in the header points to /usr/bin:/bin ).

Just use the full absolute path when you call ssmtp , then it should work.

Edit: In your crontab, you redirect stdout to /dev/null, but if the script produces output on stderr, then a message will be sent nevertheless with that output. Usually that's reasonable, as many standard unix tools are silent until there is something important to report (eg an error), in which case you want to be notified. Other tools have a 'quiet' flag or similar to reduce their verbosity which can be used in such cases.

If you don't want any message sent at all, you can redirect both stdout and stderr:

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

This way cron won't send a mail, even in case of a problem, so you should only do that after you made sure your job works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM