0

I would like to submit the same .sas job iterating over an argument, but I would like job only to be submitted after the previous is completed. Here is my attempt:

    #!/bin/ksh

    export weeks="1 2 3"
    for week in $weeks
    do

    sas -log job$week.log job.sas > /dev/null & pid_ind=$!
    wait $pid_ind

    echo "job complete for week $week"
    done

This results in all the jobs being submitted almost at once, and then they sit in the queue. Can anyone elaborate on the 'wait' command? I am trying to hold off submission until the previous SAS job was completed, not submitted.

4

You have a mistake, for loop terminated with done keyword, not fi.

for variable in lists
do
    # do something here
done
  • This should be a comment on the question, not an answer. – formixian Jun 23 '14 at 13:41
  • @Jean-PhilippeGravel: The OP had changed the question. – cuonglm Jun 23 '14 at 15:56
  • Ok, I understand. – formixian Jun 23 '14 at 16:06
0

Remove the "wait" command, there is no use for it. Aslo, remove the "& pid_ind=$!" This code puts the job into a background process that cause the problem you are experiencing.

Outside of daemon processes (and processes launched in background), commands within a script are synchronous. Each commands waits for the last one to finish.

#!/bin/ksh

weeks="3 4 5"
echo "Waiting for first job to complete..."
for week in $weeks; do
    sas -log job$week.log job.sas > /dev/null
    echo "job complete for week $week"
done
  • I've just noticed that you use /bin/ksh, I don't know how ksh behave but you should give it a try in both shells to be sure. – formixian Jun 23 '14 at 13:46
  • This is essentially what I have now, a sas job and then a sleep statement that exceeds the typical runtime of the job. This is an inefficient use of time that I'd like to tighten up. – mlegge Jun 23 '14 at 13:51
  • Ok, I got it, you use the & to put the job in the background, just do that command line, removing everything after "&": sas -log job$week.log job.sas > /dev/null – formixian Jun 23 '14 at 13:58
  • I updated my answer removing the "&" and following portion. This syntax should work as you expected now. – formixian Jun 23 '14 at 14:15
  • This is a completely different question than the one I've submitted this answer on. @mkemp6 If your goal is to execute each jobs one by one sequentially this is the right answer. On the other hand, if you want to ask a question about how the wait command works, you shoud post another question. – formixian Jun 23 '14 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.