DATA Step, Macro, Functions and more

Unix X Command Exit Code Retrieval for curl command

Reply
Occasional Contributor
Posts: 14

Unix X Command Exit Code Retrieval for curl command

Hi!  Need a little help with an unix x command and its exit code in a macro.

The command is:

x "curl -k &loc. > /var/sas/projects/analyses/data_temp.txt";

Occasionally the web address in &loc. will just 'hang'- nothing happens and the SAS program doesn't continue (I know it's a valid address).

I was thinking something like

x "timeout 2s curl -k &loc. > /var/sas/projects/analyses/data_temp.txt";

might work- if the command times out there is a non-zero exit code so doing something like "retry until exit code = 0" might work (because the macro loops through and populates &loc. with different web addresses I don't want to lose the current value of &loc.; I know the problem is isn't the value of &loc. b/c there is success during subsequent sessions and a different previously successful web address may get stuck...).

How do I get the exit code?  Is it sysrc?  What would be the best way to keep trying the current &loc. until there is success?

(This macro previously ran with no problems; being able to re-try the current &loc. and see if it gets stuck again will help figure out why a problem has recently arisen).

Thanks for any help!

Trusted Advisor
Posts: 3,214

Re: Unix X Command Exit Code Retrieval for curl command

I would look at Piping. SAS(R) 9.4 Companion for UNIX Environments

It eliminates the temp.txt file as possible cause for hang-up. Watch the locking notes.

As curl is a Unix command to transfers docs Why using that when SAS has also those interfaces (FTP SFTP URL)?

SAS(R) 9.4 Statements: Reference (sftp) When this is sufficient it wil give you more control in all options.

Do you want to use curl Unix steps think about running the logical flow system with a professional scheduler like LSF 

It will solve timings delay-s hangups etc as monitoring on process-flow proceeding is one of the goals. 

---->-- ja karman --<-----
Super User
Super User
Posts: 7,050

Re: Unix X Command Exit Code Retrieval for curl command

I find that the SAS log is much clearer if you avoid the X or SYSEXEC command and instead use a PIPE within a DATA _NULL_ so that you can capture any messages that the program you are calling was trying to send to the terminal.  You probably also need to watch for special characters in your URL that might confuse SAS or your operating system's shell.  The QUOTE() function is useful if the macro variable includes double quote characters.

%let loc=http://mysite.com ;

data _null_;

   infile %sysfunc(quote(curl -k &loc. > /var/sas/projects/analyses/data_temp.txt)) pipe ;

   input ;

   put _infile_;

run;

Occasional Contributor
Posts: 14

Re: Unix X Command Exit Code Retrieval for curl command

Thanks!  Had to use curl b/c of SSL error (used to use URL but certificates expired).  Being able to see the output to terminal in the log helps as well.

I appreciate your time!

Ask a Question
Discussion stats
  • 3 replies
  • 759 views
  • 0 likes
  • 3 in conversation