I can do anything you can do better – and with SAS

holdbackrondaNormally, things are very quiet in the De Mars household. Being out-numbered by daughters, two to one, my husband and I feel as if we need to maintain a united front against the younger generation. Last night, however, he irritated me. This was unfortunate because this is the man who, when we were dating, was referred to by the undergraduates I taught as Computer God. Why that is unfortunate is because I needed to do something and I didn’t want to go the effort of figuring out how to do it. Specifically, I wanted a job to ping two servers every day and email a message to the group the server status so if they went down, as happened in our last storms, someone would be notified when they came in that morning.

There were a half-dozen people who were discussing this and all of us knew generally that we could create a cronjob, set it to run at a specified time, have the output emailed using the Unix mail command and that job should be included in the crontab. However, as  I tell my students all of the time, when you get a real job, people don’t want you to tell them generally how to do things. They want you to do @!$( .  They don’t want a correct answer to a multiple choice question. They want code that works.

You might wonder why someone didn’t just figure this out. Well, it was the very end of the day and we all were feeling singularly unappreciated yesterday by managers who spend their time making color-coded boxes on stupid project management charts and telling us to remember to italicize headings in our documentation, so no one felt like staying the extra 15 minutes to figure it out.

[Hint: If you are a manager and you are thinking of popping by to tell me that I should not refer to your charts in my blog as “stupid”, well, I really wouldn’t do that if I were you. ]

I could have had the household Computer God do it in 5 minutes,  but when it came up that evening I was already feeling like the old song between the husband and wife, “Anything you can do, I can do better”, so when he said,

“Do you want me to write a script to run that job for you?”

I said,

“No!”  only omitting the drop dead because the little one was listening.
I guess I was particularly crabby yesterday, wasn’t I? Old people are like that.

My solution was this. We already have a SAS job that runs at 6 a.m. every day. I added this code to the bottom of it.

==========

Filename pinginf1 pipe “ping server1.school.edu” ;
Filename pinginf2 pipe “ping server2.school.edu” ;
Filename pingout “~/am_sas/pings.txt” ;
data test   ;
infile pinginf1 pad ;
input dsn $50. ;
data test2  ;
infile pinginf2 pad ;
input dsn $50. ;
data test3  ;
set test test2   ;
data _null_ ;
set test3 ;
file pingout ;
put @1 dsn ;
X ‘mail -s “Servers status ”   people@school.edu < ~/am_sas/pings.txt’ ;

=================

And it sent me the email:

server1.school.edu is alive
server2.school.edu is alive

So, done. It has the added benefit that if I DON’T get it I will know that the daily job which is supposed to run did not. I wanted this job to run on the same server and in the same file as this particular daily SAS job because it is in a group of servers that are monitored 24-7, while ours are not. It doesn’t really make sense to have a job running to test if the servers are down on a server that might go down.

There is an actual point in here regarding SAS, four actually.

  1. You can send the output of any Unix command to a SAS dataset by using the PIPE option on the FILENAME command.
  2. These datasets can be treated like any other SAS dataset.
  3. You can use the X command to execute Unix commands within a SAS job.
  4. One of the convenient uses of the X command is to execute a mail command to send yourself a file.

Yes, you can do this with a Unix script and a bunch of other ways. You can do it more elegantly in SAS. But here is how to ping your servers and have it email you the file in a few lines of code that took five minutes to write.

As for the Computer God, he just called me to say that he had done it on the Mac using automator and would show me how he did it when I got home if I want. One of my co-workers said that was nice. Still not appeased, I said it was my husband’s preference to write code as opposed to help clean up the house, and asked him,

“What would you rather do if you were married, help write a simple script or help clean?”

He said,

“Well, if I was married to you, I’d run !”

Young people are so wimpy these days. I’ m going to go home, have a glass of Chardonnay and see how to do this using automator and scheduling it through iCal. The secret to staying married is never to stay mad too long. I can’t afford to. I need him to help me over-power the children.

True fact #1: Photo above is of child #3 shortly after she had fallen down a mountain.

True fact #2: No story, no matter what precedes it, will be approved of by your mother if it ends with the line “And then I fell down a mountain.”

Similar Posts

2 Comments

  1. Hi,

    A nice way to explain the concept of X commands and use of PIPE. Do you have any better solution of scheduling the Job on unix and establishing the dependencies among them? Crontab will help us to simply schedule the job but it will fail to establish dependencies among the jobs.

    Regards,
    Amol

  2. I can do anything you can do better – and with SAS

    Wouldn’t using

    Filename pingout email options-and-parameters ;

    and skiping the X command step be simpler?

    Amol:
    Here are two choices.

    One: instead of scheduling your real job, schedule a script or program that will check conditions and take appropriate action.

    Two: in the spirit of “and with SAS,” read the documentation on the systask() and waitfor() functions in the SAS for UNIX guide.

Leave a Reply

Your email address will not be published. Required fields are marked *