Unix Programming - Re: a unix script to send email notification when a sas batch job

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2006 > Re: a unix script to send email notification when a sas batch job





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

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

2006-03-19, 12: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
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com