2

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: [email protected]
From: [email protected]
Subject: Test Alert

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

ssmtp [email protected] < /home/odroid/alerts.txt

fi

I execute this script manually and works fine, it sends the email alert to [email protected]. Here's what gmail sends out:

    Return-Path: <[email protected]>
    Received: from [email protected]
 (ppp046177171064.abcs.fe. [xx.xx.xx.xx])
            by mx.google.com with ESMTPSA id k2sm8590878wix.4.2015.04.26.12.54.57
            for <[email protected]>
            (version=TLSv1 cipher=RC4-SHA bits=128/128);
            Sun, 26 Apr 2015 12:54:59 -0700 (PDT)
    Message-ID: <[email protected]>
    Received: by [email protected] (sSMTP sendmail emulation); Sun, 26 Apr 2015 22:54:56 +0300
    Date: Sun, 26 Apr 2015 22:54:56 +0300
    To: [email protected]
    From: [email protected]
    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 <[email protected]>
X-Google-Original-From: root (Cron Daemon)
Received: by [email protected] (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 [email protected]. 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 <[email protected]>
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
3
  • Also, have you used which ssmtp to find the full path to the commend? On my Raspberry Pi it tells me it is /usr/sbin/ssmtp Commented May 10, 2017 at 23:21
  • Have you checked out the $PATH available in cron? To find out, I ran this in cron: echo $PATH | /usr/sbin/ssmtp [email protected] Commented May 10, 2017 at 23:39
  • I see in the accepted answer that @mata figured out the path - pretty clever. Commented May 10, 2017 at 23:48

1 Answer 1

2

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 (e.g. 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.

Sign up to request clarification or add additional context in comments.

4 Comments

I changed the ssmtp command line in the script to /usr/sbin/ssmtp but I get the same situation. I also noticed that some other cron jobs I have are being sent through ssmtp as well!
I have exactly the same problem, even with the absolute path the mail isn't sent. How did you fix this?
@user16655 - for starters check your logfiles (syslog, mail log, ...), there may be some information there. If that doesn't help, create a new question with detailled information (probably best on superuser, as this isn't really on topic for stackoverflow)
Good catch on finding the path in the header. Upvote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.