LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 01-04-2013, 12:07 PM   #1
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
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

Last edited by jf.argentino; 01-04-2013 at 12:09 PM. Reason: used distribution
 
Old 01-04-2013, 01:04 PM   #2
evgenyz
Member
 
Registered: Sep 2012
Posts: 48

Rep: Reputation: Disabled
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.
 
Old 01-05-2013, 05:14 AM   #3
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Original Poster
Rep: Reputation: 50
Thank you for your suggestion, I'll try it monday
 
Old 01-07-2013, 02:45 AM   #4
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Original Poster
Rep: Reputation: 50
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.
 
  


Reply

Tags
cron, empty email



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH Script will not get use result of command when run as cron job dahweeds Linux - Newbie 3 07-01-2011 07:01 PM
Backup script won't run in cron job. Why? Micro420 Linux - General 19 10-31-2007 08:26 PM
Script works when run manually but not in cron job Harlin Linux - Software 7 10-16-2007 06:36 PM
script does not run in cron job kashyapvirgo Linux - General 8 03-20-2007 10:55 AM
shell script fo run auto job in cron JolynnMarie LinuxQuestions.org Member Intro 0 04-28-2004 11:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 02:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration