I recently was asked the question, “How can you execute a command at the operating system level?”. If fact, there are times when you need a way to execute operating system commands from within SAP. Fortunately, SAP has provided several ways for us to do so.
Transaction Code: SM69 – Maintain External Operating System Commands
Although SAP provides a variety of predefined operating system commands in SM69, you can add your own commands along with their corresponding parameters. Of course, the execution and maintenance of operating system commands is protected by specific SAP authorizations.
You will find a selection of operating system commands for a number of Operating Systems available using SM69 including: ANYOS, UNIX, OS/400, AS/400, Windows NT, SunOS. Not only does SAP supply various operating system command options, you have the ability to create new ones as needed.
Executing operating system commands
You can execute operating system commands in several ways including:
- use transaction code (SM49 – Execute external OS commands)
- in an ABAP program using function modules
- as steps in a background job (SM37 – Overview of Job Selection)
- on-line in the CCMS menu
Executing operating system commands - (Using an ABAP function module)
Create a variable that contains the operating system command line including any desired paramenters. Use the call function to execute the command at the operating system level.
Function Module: SXPG_COMMAND_EXECUTE
call function 'SXPG_CALL_SYSTEM' EXPORTING commandname = 'ZABAPCMD' additional_parameters = t_date TABLES exec_protocol = t_btcxpm 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 others = 12.
Executing operating system commands – (Using an ABAP call statement)
You also can make an ABAP call to execute an operating system command. Simply create a variable that contains the operating system command line including any desired paramenters. Then use the call function to execute the command at the operating system level. This example changes the authorizations of a file at the operating system level.
v_command = 'chmod a+rwx filename'. call 'SYSTEM' id 'COMMAND' field v_command.
Executing operating system commands – (Using the OPEN DATASET statement)
The OPEN DATASET statement provides an additional way to execute operating system commands. The following code example gives you an idea of how to execute a file copy command in the UNIX operating system.
DATA: unixcmd(80) TYPE c. CONSTANTS: oldfile ..... CONCATENATE 'cp' oldfile newfile INTO unixcmd SEPARATED BY space. OPEN DATASET newfile FOR INPUT IN TEXT MODE FILTER unixcmd. CLOSE DATASET newfile.
You can also use the DELETE DATASET command to delete files at the operating system level.
DELETE DATASET filename.
As you can see there are several ways that you can execute operating system commands from within SAP. Depending upon what you are trying to do, you should be able to handle it with one of these methods.