LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (http://www.linuxquestions.org/questions/linux-server-73/)
-   -   Empty email when the script run as a cron job (http://www.linuxquestions.org/questions/linux-server-73/empty-email-when-the-script-run-as-a-cron-job-4175444169/)

jf.argentino 01-04-2013 12:07 PM

Empty email when the script run as a cron job
 
Hello,

I've got a script that check for hard-drive temperature and send an email if one of the hard-driver is too hot. This script run perfectly when running "by hand", but once added to the crontab, it mostly works, but the emails sent are empty while its subject is OK!!! I know that cron environment is not a full user environment but I can't figure where the problem stand (what environment variable I'm missing).

Here's my script:
Code:

#!/bin/sh

check_one () {
    hdd="$1"
    if [ -z "$2" ]; then
        temp_max=55
    else
        temp_max=$2
    fi
    temp=`hddtemp "$hdd" | awk -F":" '{print $3}' \
                        | sed 's/^ *//' | sed 's/[^0-9]*$//'`
    if [ "$temp" -gt "$temp_max" ]; then
        echo "$hdd reached $temp C > $temp_max C\n"
    fi
}

if [ -z "$1" ]; then
    TEMP_MAX=55
else
    TEMP_MAX=$1
fi
if [ ! -z "$2" ]; then
    EMAIL=$2
fi
WARNING="`check_one /dev/sda $TEMP_MAX`"
WARNING="$WARNING`check_one /dev/sdb $TEMP_MAX`"
WARNING="$WARNING`check_one /dev/sdc $TEMP_MAX`"
if [ ! -z "$WARNING" ]; then
    if [ -z "$EMAIL" ]; then
        echo -e "$WARNING"
    else
        echo -e $WARNING | mailx -s 'Harddrive temperature' $EMAIL
        #TMPFILE=`mktemp`
        #echo -e $WARNING > "$TMPFILE"
        #mailx -s 'Harddrive temperature' $EMAIL < "$TMPFILE"
        #rm "$TMPFILE"
    fi
fi

My "/etc/ssmtp/ssmtp.conf" should be OK since the emails are received, but maybe something's missing there due to cron?
Code:

root=jf.argentino@email.fr
AuthUser=jf.argentino@email.fr
AuthPass=passwd
UsesTLS=YES
UsesSTARTTLS=YES
mailhub=ssl0.ovh.net:465

"/etc/ssmtp/revaliases" is empty

and by crontab:
Code:

0-59/5 * * * *  /usr/bin/hddtemp.sh 55 jf.argentino@email.fr
If I'm storing the message to send into a file (commented in the script above), the email is still empty...

If I do not erase the temporary file, the email is still empty, but the file contents the message I want to send.

If the message content is not in a variable, I'm receiving it...

So it looks like my problem is relative to both variable and redirection handling by cron, but I can't fix this.

Does anybody have an idea?

My system is a Scientific Linux 6.3 up to date

evgenyz 01-04-2013 01:04 PM

Try to execute /etc/profile in the head of the script such as:

#!/bin/sh
. /etc/profile

I had such problem and this fixed it.

jf.argentino 01-05-2013 05:14 AM

Thank you for your suggestion, I'll try it monday

jf.argentino 01-07-2013 02:45 AM

Thank you it works.
But if anybody can explain to me how a variable content can disappear when piped, i'll be happy...
Ahhh bash black magic.


All times are GMT -5. The time now is 12:29 AM.