Find the answer to your Linux question:
Results 1 to 3 of 3
Hi All, I'm fairly new to bash scripting, so thanks in advance for your help and if this question is more appropriate somewhere else please let me know. I'm trying ...
  1. #1
    Just Joined!
    Join Date
    Mar 2009
    Posts
    2

    Bash script to execute and email job status

    Hi All, I'm fairly new to bash scripting, so thanks in advance for your help and if this question is more appropriate somewhere else please let me know.

    I'm trying to write a script that will execute a job, email both the standard out and error and weather or not it was successfully completed. I think it would be fairly simple if I just redirected the standard out and error to a file but I'm trying to avoid that because I will have potentially hundreds of jobs running at the same time and I don't want them to be overwriting each other. So I've tried to just pipe the output to mail.

    The problem is the command always exits with a zero because the mail program executes successfully even if the original program failed. Any ideas? Here's what I got so far:

    #! /bin/bash

    myCommand=$1
    eval $myCommand 2>&1 | mail -s "Executed: $myCommand" myEmail

    if [ $? = 0 ]
    then
    echo "Successful: $myCommand"
    #mail -s "Successful: $myCommand" myEmail
    else
    echo "FALIURE: $myCommand"
    #mail -s "FALIURE: $myCommand" myEmail
    fi

    exit

  2. #2
    Linux User Felarin's Avatar
    Join Date
    Mar 2007
    Location
    Brazil or Singapore
    Posts
    314
    Try putting a && behind the first command.
    "A graphical user interface is just a mask. What lies beneath is what matters."

  3. #3
    Just Joined!
    Join Date
    Mar 2009
    Posts
    2
    I figured it out, needed to use the following:

    exitStatus=${PIPESTATUS[0]}

Posting Permissions

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