Re: a unix script to send email notification when a sas batch job
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Re: a unix script to send email notification when a sas batch job




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Re: a unix script to send email notification when a sas batch job  
Logan Shaw


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-19-06 05:02 PM

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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:30 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register