Saturday, October 6, 2012

Running OS Program or Script as SAP Job Step




SAP allows you to trigger an OS command or script through a job step. A very common usage of this facility is to write FTP scripts and use them as a job step to transfer files.



When the background process wants to trigger the script, it sends the request out to SAP gateway (local sapgwXX) through the dispatcher. The gateway calls sapxpg with the OS script/command as an argument. sapxpg then executes the OS command/script.



When the command/script finishes or outputs intermediate messages, it passes the information to sapxpg; sapxpg passes that to sapgw; sapgw to the background process via dispatcher.



If any of the commands in your script fails and you do not handle them with proper exit codes from the script, the job will be shown as successful as the script exits with an exit 0 command.



For example:



If we use the following ftp script:



#!/bin/ksh

ftp -vn $HOST <<END_FTP > /ftp_working_directory/ftplog 2>&1

quote USER sapnwnewbie

quote PASS easytocrack

ascii

get filename.txt

bye

END_FTP

exit 0



Let us assume that the ftp user is locked; the ftp command fails as a result. But the script ends with an exit 0 and the job is marked as successful though the objective of the job is not met.



You have to use the $? variable to determine if the ftp was successful and exit the script accordingly.



#!/bin/ksh

echo "FTP command execution begins..."

ftp -vn $HOST <<END_FTP > /ftp_working_directory/ftplog 2>&1

quote USER sapnwnewbie

quote PASS easytocrack

ascii

get filename.txt

bye

END_FTP

EXIT_STATUS=$?

if [[ $EXIT_STATUS -ne 0 ]]

then

echo "FTP command has failed, check /ftp_working_directory/ftplog on `hostname`"

exit 1

else

echo "FTP command was successful"

fi

exit 0



This is just an example. When using an OS script, think of possible failures and handle the errors in the script carefully. It is the script's responsibility to pass appropriate exit code to sapxpg. This exit code reflects in jobs status.


No comments:

Post a Comment