Hi everybody!
Since few weeks ago I am working in a project where I have to carry on some repetitive tasks on a SAP system (a Production clone).
First of these tasks is check if SAP is up and running (if not, I have to restart it) so I've developed a python script to automatize it and now I would like to share it with you.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import subprocess
import shlex
CMD_SAP_CHECK = "startrfc -3 -u < USER > -p < PASSWORD > -c < CLIENT > -l < LANG > " \
"-h < SAPMSGSERVER > -s < INST.NUM > -F SUBST_START_BATCHJOB " \
" -E JOBNAME=CHECK_IF_SAP_IS_UP -E REPNAME=SAPMSM66"
CMD_SAP_START = "startsap < SAPMSGSERVER > all"
CMD_SAP_STOP = "stopsap < SAPMSGSERVER > all"
def execute(cmd):
try:
args = shlex.split(cmd)
process = subprocess.Popen(args, stderr=subprocess.PIPE)
process.wait()
return process
except Exception, error:
print "ERROR: ", error
raise
if __name__ == "__main__":
process = execute(CMD_SAP_CHECK)
if process.returncode != 0:
log = open("checksap.log", "w")
log.write(" RC: %d\n" % process.returncode)
log.write("STDERR:\n%s" % process.stderr.read())
log.close()
execute(CMD_SAP_STOP)
execute(CMD_SAP_START)If the script is able to launch the background job it will return return code 0.
If not, it will return another number and it will write stderr output to checksap.log file:
RC: 1
STDERR:
RFC Call/Exception: SYSTEM_FAILURE
Group Error group 104
Key RFC_ERROR_SYSTEM_FAILURE
Message Name or password is incorrect (repeat logon)
Then, it will stop SAP system and after that, start it again.
Some notes: