BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bsas94
Obsidian | Level 7

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,

1 ACCEPTED SOLUTION

Accepted Solutions
ErikLund_Jensen
Rhodochrosite | Level 12

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

 

 

 

 

View solution in original post

1 REPLY 1
ErikLund_Jensen
Rhodochrosite | Level 12

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

 

 

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 821 views
  • 0 likes
  • 2 in conversation