0

I want to call shell script from python code. The shell script which I am trying to call is having multiple database (DB2) call in it; means it connects to DB2 database multiple times and execute different database sqls. I tried using subprocess.call method like (subprocess.call(['./<shell script with full path>'])); but it seems before the script connects to database and executes the commands mentioned within the script, it is terminating. But when I am calling the shell script as a standalone script from command line, then it is working good.

Is there any other way this can be handled?

mao
  • 11,321
  • 2
  • 13
  • 29
Koushik Chandra
  • 1,565
  • 12
  • 37
  • 73
  • You are using a relative path (contains . ) , how are you guaranteeing that $PWD is correct? Next, does the shell script correctly dot in the db2profile? Do Some debugging. – mao Oct 10 '19 at 08:09

1 Answers1

0

subprocess: The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.

http://docs.python.org/library/subprocess.html

Usage:

import subprocess
process = subprocess.Popen(command, shell=True)
process.wait()
print process.returncode

Side note: It is best practice to avoid using shell=True as it is a security hazard.

Actual meaning of 'shell=True' in subprocess