3

I have 5 SAS jobs that I need to run sequentially, one after the other.

I typically type in nohup sas filename1.sas & in the command line to run and manually check for progress every few hours. If the 1st job is complete and no error, I then type in the 2nd job nohup sas filename2.sas & .

Is there a sas code or unix command I can run them sequentially rather than manually checking progress?

I thought about using %include statement in a master sas file, however I have many loop macros and do if then macros which throw the %include off I believe.

PS. I also need the log and lst file to be printed, typically it's printed for me automatically using the command above.

1
  • 1
    How do you determine if a job has completed properly with no error? Commented Aug 15, 2016 at 21:49

1 Answer 1

0

there are a couple of ways you can check if you have jobs running. Lets say you ran the command

nohup sas filename1.sas &

you should see something like this in return:

[1] 7539
[mel@server] $ nohup: ignoring input and appending output to `nohup.out'

you can check the existence of process number 7539 using an infinite loop as follows:

PID=${!} # this must be run immediately after submitting your job
flag=0
while [ $flag -eq 0 ]
do
  sleep 30    
  ps -ef | grep ${PID} | grep -v grep >/dev/null
  flag=${?}
done
echo "process ${PID} completed or died"

or you can do something in more crude way:

flag=1
while [ ${flag} -ne 0 ]
do
  sleep 30
  flag=$(jobs|wc -l)
done
echo "all background jobs have finished or died"

either of these methods will check existence of your background jobs every 30 seconds and completes when there is no job running in the background. First method is my preference.

EDIT (Per comments below):

To run all 5 jobs running one after the other, regardless of success or failure of preceding job, you can do this (note that, code below assumes your sas job filenames are in the format of filename1.sas , filename2.sas , ... , filename5.sas):

>nohup.out
for i in 1 2 3 4 5
do
  nohup sas filename${i}.sas &
  PID=${!} 
  flag=0
  while [ $flag -eq 0 ]
  do
    sleep 30    
    ps -ef | grep ${PID} | grep -v grep >/dev/null
    flag=${?}
  done  # end of while loop
  echo "process ${PID} completed or died"
  mv nohup.out filename${i}.log # preserve a separate log file for each job
  # if you know the successful and failed exit codes of sas process, 
  # you can compare the result to those values here. Since you did not 
  # provide any exit codes, this is left as is.
done  # end of for loop
6
  • I'm actually looking for a method that I can run code 1, and once code 1 its done, I want to run code 2, then once its done, run code 3.
    – SuperKing
    Commented Aug 16, 2016 at 1:48
  • Then, you need to know what happens when your job dies or completes with errors, i.e., unsuccessful run. And the difference between a successful and unsuccessful run. Or doesn't it matter if it is successfull or not, you just need to run the next job ?
    – MelBurslan
    Commented Aug 16, 2016 at 13:56
  • It does not make too much of a difference, would be nice if I know if a job die but priority is to run the next job
    – SuperKing
    Commented Aug 16, 2016 at 19:10
  • Please see the last block under EDIT header for running all 5 jobs in sequence.
    – MelBurslan
    Commented Aug 16, 2016 at 19:30
  • while ps -p ${pid} >&-; do sleep 30; done
    – Larry
    Commented Apr 12, 2020 at 18:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .