Using Operating System Commands in ABAP

ABAPAnthony Cecchini is the President of Information Technology Partners (ITP), an SAP consulting company headquartered in Pennsylvania, with offices in Vienna, VA. ITP offers comprehensive planning, resource allocation, implementation, upgrade, and training assistance to companies. Anthony has over 20 years of experience in SAP business process analysis and SAP systems integration. His areas of expertise include SAP NetWeaver integration; ALE development; RFC, BAPI, IDoc, Dialog, and Web Dynpro development; and customized Workflow development. You can reach him at ajcecchini@itpsap.com.

Interacting with the HOST OS using ABAP

SAP does such a good job of abstracting its surrounding environment that we rarely take the features of its host operating system into account. The reality is that ABAP isn’t particularly good at solving certain problems. Rather than trying to reinvent the wheel with convoluted solutions, it’s best to solve these problems with the right tool. And sometimes, that tool is sitting on the operating system just waiting to be used.

For instance, at one of our current clients, I was working on their EDW team and needed to archive a file from a “process” directory to an “archive” directory after processing. I was surprised to see a relatively seasoned developer was using ABAP logic to achieve this. They were opening a new file in the “archive” directory, writing the contents, and closing the file and finally deleting the file from the “processing” directory. I changed the code to utilize the UNIX MOVE command. It was much less code and leveraged the existing UNIX Command.

In this blog, I want to show you how to interact with the host operating system of SAP NetWeaver AS ABAP. Specifically, I’ll introduce you to a framework that SAP provides as part of the standard to define external commands in a highly portable manner. After explaining the basics of this framework, we will look at an example of how to define and use the external commands.

Programming in ABAP with External Commands

Unlike other programming interfaces provided in ABAP, there is no built-in language statement that you can use to execute external commands. Instead, you must define these commands within the system so that they can be executed via standard API functions. Let’s begin by exploring how to maintain the external commands.

Maintaining External Commands

OS Host External commands are maintained using Transaction SM69. As you can see from the screen shot below, a lot of commands are delivered by SAP in every NetWeaver system; consequently, it’s always a good idea to see if SAP has already configured the command you’re looking to execute instead of creating a new command definition from scratch.

SM69 Maintain External Commands

OK, let’s take a deeper look. In order to show you how to configure your own custom external commands, let’s look at one delivered by SAP. The PING command is available on any SAP NetWeaver AS ABAP host. The PING command will determine whether an IP address is reachable on a network.

Put your cursor on the PING command as shown above and double-click or hit the display icon.

Ping Command

As you can see in screen shot above, the definition of an external command is broken up into two distinct parts.

Fields in the COMMAND Group Box

In the Command group box, you must configure the following:

– In the Command name field, you must assign a unique name to the command. This name must be defined in the proper namespace (i.e., prefixed with a Y or Z for the customer namespace, etc.).

– The Operating System field defines the operating system or systems that support this command. In the case of the PING command, the generic ANYOS value was assigned to indicate that the PING command can be run on any host operating system. However, because some external commands are OS-specific, this field is necessary.

– The Type field refers to whether or not the command was defined by SAP or a customer.

Fields in the DEFINITION Group Box

The actual definition of the command occurs within the Definition group box. Here you define the following:

– In the Operating System Command field, define the command to be executed. This can be a built-in OS command, a path to an executable on the host system, and so on.  In our case “PING”

– In the Parameters for Operating System Command field, you can define parameters to be passed to the OS command at runtime. These parameters are static parameters that will always be passed to the command.

– If you want to allow dynamic parameters to be passed into the command, you need to select the Additional Parameters Allowed check box. These parameters are passed to the command at runtime using the API functions.

– The Trace checkbox determines whether or not you want the framework to turn on a trace when the command is being executed.

– In the Check Module input field, you can plug in a function module that is called by the framework to validate the external command before executing it. This function module must match the signature of the SXPG_DUMMY_COMMAND_CHECK function module provided with the system. This will run before the command is executed and if an exception is raised by the check module, the external command isn’t executed. This works like an EVENT or Workflow Check Function.

Testing The External Command

OK, so we want to test an SAP delivered command, or we just created a “Z” Command and we would like to test it. To test an external command, select the command and click on the Execute button in the toolbar Code Inspector

Ping Test

Here, you have the option of plugging in test parameters in the Additional Parameters field. You can also select an execution target to test the command on other application server hosts. I will use Yahoo’s site, and attempt to PING it. When you click on the Execute button, the external command is executed, and you’re navigated to the results screen shown below. Here, you can see the return code of the command, as well as the generated output.

Ping Test Results

 

Executing External Commands in an ABAP

To execute external commands within an ABAP program, you can use the standard function module SXPG_COMMAND_EXECUTE. To demonstrate how this works, let’s code a quick little DEMO program. This program accepts a host name as a parameter and uses it as an argument to the PING command. The PING command is executed via a call to the standard API function. The code is below.

REPORT zdemo_ping.
 CLASS lcl_command_interface DEFINITION.
   PUBLIC SECTION.
     CLASS-METHODS:
       execute_command IMPORTING im_command_name
                                   TYPE sxpgcolist-name
                                 im_param_string
                                   TYPE btcxpgpar.
 ENDCLASS.
 CLASS lcl_command_interface IMPLEMENTATION.
   METHOD execute_command.
     "Method-Local Data Declarations:
     DATAlv_status  TYPE extcmdexex-status,
           lv_retcode TYPE extcmdexex-exitcode,
           lt_output  TYPE STANDARD TABLE OF btcxpm.
     FIELD-SYMBOLS:
     <lfs_output> LIKE LINE OF lt_output.
     "Execute the selected command:
     CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
       EXPORTING
         commandname                   im_command_name
         additional_parameters         im_param_string
         operatingsystem               'ANYOS'
       IMPORTING
         status                        lv_status
         exitcode                      lv_retcode
       TABLES
         exec_protocol                 lt_output
       EXCEPTIONS
         no_permission                 1
         command_not_found             2
         parameters_too_long           3
         security_risk                 4
         wrong_check_call_interface    5
         program_start_error           6
         program_termination_error     7
         x_error                       8
         parameter_expected            9
         too_many_parameters           10
         illegal_command               11
         wrong_asynchronous_parameters 12
         cant_enq_tbtco_entry          13
         jobcount_generation_error     14
         OTHERS                        15.
     "Display the results of the command:
     LOOP AT lt_output ASSIGNING <lfs_output>.
       WRITE/ <lfs_output>-message.
     ENDLOOP.
   ENDMETHOD" METHOD execute_command
 ENDCLASS.
 PARAMETERS:
 p_host TYPE btcxpgpar LOWER CASE OBLIGATORY.

 START-OF-SELECTION.
   "Execute the 'PING' command:
   lcl_command_interface=>execute_command(
   im_command_name 'PING'
   im_param_string p_host ).

Let’s execute the program… You will the Selection Screen below asking for a Host Name.

Zdemo Ping Program

Let’s use www.yahoo.com again and execute… The results are displayed below.

zdemo ping results

 

Summary

As you can see, executing and interacting with the host operating system in ABAP is easy to do. SAP has created a framework for you to first configure via SM69 and then use via the delivered API SXPG_COMMAND_EXECUTE.

ITP logo

If you enjoyed this blog, Using Operating System Commands in ABAP, please fill out the form below to sign up for our newsletter. We deliver SAP Technical tips & tricks, SAP news, and the current month’s BLOG right to your inbox!

Related Posts

Pin It on Pinterest

Share This

If you enjoyed this post, why not share it with your friends!