a unix script to send email notification when a sas batch job fails?

  • Follow


Hi,

This post may be a bit off topic, but essentially it's mainly about
unix script, not SAS (a statistical software). (but I posted it on
comp.soft-sys.sas too hoping for any clue)

My OS is RedHat Enterprise. When I want to run a sas code (say
freq.sas) in background on our server, I simply need to type in command
line "sas freq.sas &".

SAS documentation says that:

"an Exit Status Code can be used to determine the Completion Status of
a SAS Job in UNIX Environments."

and

"The exit status for the completion of a SAS job is returned in $status
for the C shell, and in $? for the Bourne and Korn shells. A value of 0
indicates normal termination."

So I'm wondering if this information could be used to write a short
simple unix script which is able to do the following 2 things:
1, invoke a sas job in batch mode
2, send me an email if this sas batch job fails

Say the name of this unix script is "AutoNotify", and the sas code I
want to run is "freq.sas".

And I hope this script can function this way:
when I type "./AutoNotify freq.sas", it will
1, run freq.sas in background,
2, and will send me an email if the Exit Status Code is not
zero.

Any input is welcome, an sample of such script would be greatly
appreciated. (not a homework question, I just know very little about
unix scripting)

Thank you very much in advance !!!

0
Reply greenmt (57) 3/16/2006 12:20:17 AM

"Jerry" <greenmt@gmail.com> writes:

> Hi,
>
> This post may be a bit off topic, but essentially it's mainly about
> unix script, not SAS (a statistical software). (but I posted it on
> comp.soft-sys.sas too hoping for any clue)
>
> My OS is RedHat Enterprise. When I want to run a sas code (say
> freq.sas) in background on our server, I simply need to type in command
> line "sas freq.sas &".
>
> SAS documentation says that:
>
> "an Exit Status Code can be used to determine the Completion Status of
> a SAS Job in UNIX Environments."
>
> and
>
> "The exit status for the completion of a SAS job is returned in $status
> for the C shell, and in $? for the Bourne and Korn shells. A value of 0
> indicates normal termination."
>
> So I'm wondering if this information could be used to write a short
> simple unix script which is able to do the following 2 things:
> 1, invoke a sas job in batch mode
> 2, send me an email if this sas batch job fails
>
> Say the name of this unix script is "AutoNotify", and the sas code I
> want to run is "freq.sas".
>
> And I hope this script can function this way:
> when I type "./AutoNotify freq.sas", it will
> 1, run freq.sas in background,
> 2, and will send me an email if the Exit Status Code is not
> zero.
>
> Any input is welcome, an sample of such script would be greatly
> appreciated. (not a homework question, I just know very little about
> unix scripting)

You can learn more about unix scripting.  Read: man bash

#!/bin/bash
address="your.self@your.host"
if [ $# = 1 ] ; then
   "$1" || printf "Script %s failed with status %s" "$1" "$?" \
           | mail -s "notification" "$address"
    exit $?   # or exit 0
else
    printf "Usage:\n   AutoNotify program\n"
    exit 1
fi


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Kitty like plastic.
Confuses for litter box.
Don't leave tarp around.
0
Reply Pascal 3/16/2006 12:31:25 AM


"Jerry" <greenmt@gmail.com> writes:

> So I'm wondering if this information could be used to write a short
> simple unix script which is able to do the following 2 things:
> 1, invoke a sas job in batch mode
> 2, send me an email if this sas batch job fails

sas || echo 'SAS failed' | mail -s 'SAS FAIL' 'me@somewhere' &

-- 
Roger Leigh
                Printing on GNU/Linux?  http://gutenprint.sourceforge.net/
                Debian GNU/Linux        http://www.debian.org/
                GPG Public Key: 0x25BFB848.  Please sign and encrypt your mail.
0
Reply Roger 3/16/2006 12:42:08 AM

On 15 Mar 2006 16:20:17 -0800, Jerry wrote:
>
> Any input is welcome, an sample of such script would be greatly
> appreciated. (not a homework question, I just know very little about
> unix scripting)

These might help

! bash script introduction documentation
http://tldp.org/LDP/intro-linux/html/index.html 

! bash script advanced documentation
http://tldp.org/LDP/abs/html/index.html         
0
Reply Bit 3/16/2006 1:36:32 AM

Jerry <greenmt@gmail.com> wrote:
> Hi,
> 
> This post may be a bit off topic, but essentially it's mainly about
> unix script, not SAS (a statistical software). (but I posted it on
> comp.soft-sys.sas too hoping for any clue)
> 
> My OS is RedHat Enterprise. When I want to run a sas code (say
> freq.sas) in background on our server, I simply need to type in command
> line "sas freq.sas &".
> 
> SAS documentation says that:
> 
> "an Exit Status Code can be used to determine the Completion Status of
> a SAS Job in UNIX Environments."
> 
> and
> 
> "The exit status for the completion of a SAS job is returned in $status
> for the C shell, and in $? for the Bourne and Korn shells. A value of 0
> indicates normal termination."
> 
> So I'm wondering if this information could be used to write a short
> simple unix script which is able to do the following 2 things:
> 1, invoke a sas job in batch mode
> 2, send me an email if this sas batch job fails
> 
> Say the name of this unix script is "AutoNotify", and the sas code I
> want to run is "freq.sas".
> 
> And I hope this script can function this way:
> when I type "./AutoNotify freq.sas", it will
> 1, run freq.sas in background,
> 2, and will send me an email if the Exit Status Code is not
> zero.
> 
> Any input is welcome, an sample of such script would be greatly
> appreciated. (not a homework question, I just know very little about
> unix scripting)
> 
> Thank you very much in advance !!!

Solution:
    sas freq.sas || mail -s "SAS job failed" your@address < /dev/null

Wait... I gave away solution for free... doh!

-- 
William Park <opengeometry@yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
	   http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
	  http://freshmeat.net/projects/bashdiff/
0
Reply William 3/18/2006 12:02:19 AM

Jerry wrote:

> This post may be a bit off topic, but essentially it's mainly about
> unix script, not SAS (a statistical software). (but I posted it on
> comp.soft-sys.sas too hoping for any clue)
> 
> My OS is RedHat Enterprise. When I want to run a sas code (say
> freq.sas) in background on our server, I simply need to type in command
> line "sas freq.sas &".
> 
> SAS documentation says that:
> 
> "an Exit Status Code can be used to determine the Completion Status of
> a SAS Job in UNIX Environments."
> 
> and
> 
> "The exit status for the completion of a SAS job is returned in $status
> for the C shell, and in $? for the Bourne and Korn shells. A value of 0
> indicates normal termination."
> 
> So I'm wondering if this information could be used to write a short
> simple unix script which is able to do the following 2 things:
> 1, invoke a sas job in batch mode
> 2, send me an email if this sas batch job fails

I think you should consider using the "batch" command, whose purpose is
to run something in batch mode and then send you an e-mail with the
command's output.  "batch" takes a shell command on its standard input,
so an easy way to do what you want is this:

     echo "sas freq.sas" | batch

"batch"'s behavior is such that if your command produces no output,
then an e-mail is not sent.  If your command produces no output
normally and you'd like to be notified if it fails, you could solve
this problem by using the "||" conditional shell operator.

Briefly, the "||" operator is a lazy "or", so it will only run the
second command if the first fails.  Since "true" is a command that
always succeeds (well, always tries to succeed...) and "false" is
a command that always fails, you can easily try this out on the
command line.  Try the following two commands:

     true || date

     false || date

You should see that the first one doesn't print the date and the
second one does.  That's because "true" succeeds; therefore, "date"
isn't run.  And the opposite happens with "false".  You can use a
similar construct to get a notification if your command fails:

     sas freq.sas || date

That would print the date if the "sas" command fails.  But, you'd
probably rather have an informative message, so you could use the
"echo" command instead:

     sas freq.sas || echo failed

This should print the string "failed" if the command fails.  You
might want to know the exit code for diagnostic purposes, and the
shell puts that (i.e. the last command's exit code) in the special
"$?" variable, so you could expand the above to this:

     sas freq.sas || echo "failed with code $?"

If "sas" has a habit of printing output that you don't care about,
you could redirect standard output to /dev/null to stop that:

     sas freq.sas >/dev/null || echo "failed with code $?"

And, you might want to redirect standard error as well (or you might
not, actually, depending on how the command behaves, but let's assume
you do want to):

     sas freq.sas >/dev/null 2>/dev/null || echo "failed with code $?"

By redirection file #2 to the same place as file #1 (since ">" is
shorthand for "1>"), we can be a little more concise:

     sas freq.sas >/dev/null 2>&1 || echo "failed with code $?"

And if you want to have the batch command process all this, then
you just need to send this command, unmodified, into the standard
input of the "batch" command, in this case through a pipe:

     echo 'sas freq.sas >/dev/null 2>&1 || echo "failed with code $?"' | batch

Hope that helps.

   - Logan
0
Reply Logan 3/18/2006 9:20:21 PM

5 Replies
1225 Views

(page loaded in 0.525 seconds)

Similiar Articles:













7/22/2012 1:15:21 PM


Reply: