BookmarkSubscribeRSS Feed
dakes
Obsidian | Level 7

Hi,

 

I wrote a bit of code to check whether the same code is already executing. If it was, stop the new run, if it doesn't start the new run as expected.

 

%macro checkifrunning;
x ' ps -ef|grep boxoffice|grep -v grep';
%let rc=&sysrc ;
%put &=rc ;

%if &rc. eq 0 %then %do;
	%put Another instance of this code is running, stopping execution;
	%Abort Return;
	%end;
%else %do;
	%put No other instance of this code is running;
%end;

%put continuing execution;
%mend;
%checkifrunning;

A bit of background:

ps -ef|grep boxoffice|grep -v grep 

 

On linux (which we run) this has a return code ($?) of 0 when the process (boxoffice) is running and 1 when it isn't. This works perfectly well when I run it in SAS Enterprise Guide., but when a run is started by SAS Management console the return code is always 0.

 

Can anybody tell me what I am doing wrong?

 

Thanks for the help!

3 REPLIES 3
Kurt_Bremser
Super User

When you run the code in EG, the SAS process has no "jobname" in its commandline. The workspace server is started on demand from EG, but the current "program name" does only exist as a macro variable within the Base SAS environment.

When you run it from SMC (scheduled), it runs in batch mode, and the program name will be on the commandline, so the job finds itself.

 

Write a test program, and use this:

filename oscmd pipe "ps -ef|grep test.sas|grep -v grep";

data _null_;
infile oscmd;
input;
put _infile_;
run;

to catch all output from your command. This will tell you how many lines the grep finds, from which you can design suitable code.

dakes
Obsidian | Level 7

Tank you or your reply. Even though I didn't really understand what the jobname has to do with SAS running grep on the linux box I think your answer pointed me into the right direction:

 

filename oscmd pipe "ps -ef|grep boxoffice|grep -v grep";

data _null_;
if eof then 
	do;
		put "Lines read:" _n_;
		if _n_ gt 1 then
			do;
				put "Another instance of this code is running, stopping execution";
				/*abort 255;*/
			end;
		else
			do;
				put "No other instance of this code is running, continuing";
			end;	
	end;
infile oscmd end=eof;
input;
put _infile_;
run;

I had to put the check before the infile-statement because otherwise "if eof" wouldn't trigger when the file is empty (grep returned 0 lines), which means even if grep returns 0 lines _n_ will be 1. Is that the recommended way of working around that issue?

 

Thank you very much for your help!

 

 

/edit: Ok, I got it now and I feel incredibly stupid... Of course the process finds itself when it is started by SMC.... I changed the code to gt 2.

 

Thank you all for your help!

s_lassen
Meteorite | Level 14

Have you checked the XCMD option on the SAS session that you start from the management console? If it is NOXCMD, that may explain the problem (the default value for &sysrc is 0, so you may get that if no X command is run).

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 3 replies
  • 766 views
  • 1 like
  • 3 in conversation