Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, I am trying to execute a script from crontab and having issues with getting the script to send out an email. This is my cron expression: 1 12 * ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2011
    Posts
    12

    CRONTAB Email issue


    Hi,

    I am trying to execute a script from crontab and having issues with getting the script to send out an email.

    This is my cron expression:
    1 12 * * * /home/Testing/Client/TST.sh -l

    The TST.sh script executes a bunch of tests scripts and then sends the resulting output to another script, errors.sh. The Errors.sh script parses the output and is supposed to formulate an email message and then send it out.

    if i execute the TST.sh script from the commandline the scripts run as expected and sends out the email correctly. It is only when i try to run it as a cron job does the email not work. When the job runs, the scripts seem to run fine only the email ends up in /var/spool/mail/userid - instead of sending the email out. the email that gets sent to this file also contains the incorrect information.

    How can i get the errors.sh to send out the email?

    Here is the bit of code i use to send out the email in errors.sh after the output from TST.sh has been parsed:

    Code:
    # script to send simple email
    # Email To ?
    EMAIL="email(at)address.com"
    # Email text/message
    EMAILMESSAGE="/tmp/emailmessage.txt"
    echo "Test script completed at: $DATE" > $EMAILMESSAGE
    echo " $header " >> $EMAILMESSAGE
    echo " $results " >>$EMAILMESSAGE
    # send an email using /bin/mail
    /bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
    NOTE: email(at)address.com is an actually email address it wont let me use the 'at' symbol.
    Last edited by dbo3587; 01-25-2012 at 07:55 PM.

  2. #2
    Just Joined!
    Join Date
    Jan 2012
    Location
    Rowlett, TX
    Posts
    5
    Instead of using /bin/mail to mail out the results, let cron send them to you. All output (stdout and stderr) produced by the script will be emailed to you.

    All you have to do is set the MAILTO variable in your crontab.

    Update your script to something like:
    Code:
    # script to send simple email
    # Email To ?
    EMAIL="email(at)address.com"
    # Email text/message
    EMAILMESSAGE="/tmp/emailmessage.txt"
    echo "Test script completed at: $DATE" > $EMAILMESSAGE
    echo " $header " >> $EMAILMESSAGE
    echo " $results " >>$EMAILMESSAGE
    # send everything to stdout
    cat $EMAILMESSAGE
    Set the MAILTO variable in your crontab:
    Code:
    MAILTO=penguin@linux.com
    The next time cron runs, the contents of "/tmp/emailmessage.txt" will be mailed to the address set in MAILTO.

    Hope this helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •