Hi experts,
I have a sas program that executes a bash in Linux:
%macro test;
%do i = 1 %to 10;
data _null_;
extract = "/opt/tst/extract.sh" "xml&i.";
call system (extract);
run;
%end;
%mend;
%test;
Is there any way I can skip/terminate the current bash program when the interation takes more than x seconds?
For example:
timeout =40 seconds
Iteration =1
command took 30 seconds to complete
Iteration = 2
command is still executing and it has reached 40 seconds
terminate bash execution
iteration = 3
command took 35 seconds to complete
--
Any inputs?
Thanks,
Hi @bsas94
Use the linux timeout command.
As an example I made 2 small scripts, test1.sh (takes 10 seconds to finish, and test2.sh (takes 20), and call them with the following macro, that is similar to yours with logging of iteration time added. The timeout command returns 124 if the command is timed out, otherwise the return code from the executed command.
%macro test;
%do Iteration = 1 %to 2;
%let runstart = %sysfunc(time());
%sysexec timeout 15s sh "/sasdata/prod/system/temp/test&Iteration..sh";
%let runtime = %sysfunc(round(%sysevalf(%sysfunc(time())-&runstart)));
%if &sysrc=124 %then %put &=Iteration - Command terminated after &runtime seconds;
%else %put &=Iteration - Command completed in &runtime seconds;
%end;
%mend;
%test;
result:
69 %macro test; 70 %do Iteration = 1 %to 2; 71 %let runstart = %sysfunc(time()); 72 %sysexec timeout 15s sh "/sasdata/prod/system/temp/test&Iteration..sh"; 73 %let runtime = %sysfunc(round(%sysevalf(%sysfunc(time())-&runstart))); 74 %if &sysrc=124 %then %put &=Iteration - Command terminated after &runtime seconds; 75 %else %put &=Iteration - Command completed in &runtime seconds; 76 %end; 77 %mend; 78 %test; ITERATION=1 - Command completed in 10 seconds ITERATION=2 - Command terminated after 15 seconds
Hi @bsas94
Use the linux timeout command.
As an example I made 2 small scripts, test1.sh (takes 10 seconds to finish, and test2.sh (takes 20), and call them with the following macro, that is similar to yours with logging of iteration time added. The timeout command returns 124 if the command is timed out, otherwise the return code from the executed command.
%macro test;
%do Iteration = 1 %to 2;
%let runstart = %sysfunc(time());
%sysexec timeout 15s sh "/sasdata/prod/system/temp/test&Iteration..sh";
%let runtime = %sysfunc(round(%sysevalf(%sysfunc(time())-&runstart)));
%if &sysrc=124 %then %put &=Iteration - Command terminated after &runtime seconds;
%else %put &=Iteration - Command completed in &runtime seconds;
%end;
%mend;
%test;
result:
69 %macro test; 70 %do Iteration = 1 %to 2; 71 %let runstart = %sysfunc(time()); 72 %sysexec timeout 15s sh "/sasdata/prod/system/temp/test&Iteration..sh"; 73 %let runtime = %sysfunc(round(%sysevalf(%sysfunc(time())-&runstart))); 74 %if &sysrc=124 %then %put &=Iteration - Command terminated after &runtime seconds; 75 %else %put &=Iteration - Command completed in &runtime seconds; 76 %end; 77 %mend; 78 %test; ITERATION=1 - Command completed in 10 seconds ITERATION=2 - Command terminated after 15 seconds
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: