0

I'm running a DB2 stored procedure from Python using subprocess.run. The command works perfectly when I run it directly in my shell:

db2 'CALL GET_DBSIZE_INFO(?, ?, ?, 0);' | awk -F': *' '/Parameter Value/ { print $2 }'

It returns the expected value when run manually:

2025-11-25-06.20.33.191132
3702824960
56861528064

However, when I run the same command through Python:

import subprocess

con = "db2 connect to sample user **** using ****"
cmd = "db2 'CALL GET_DBSIZE_INFO(?, ?, ?, 0);' | awk -F': *' '/Parameter Value/ { print $2 }'"
res1 = subprocess.run(con, shell=True, capture_output=True, text=True)
res2 = subprocess.run(cmd, shell=True, capture_output=True, text=True)
print(res2.stdout)
print(res2.stderr)

I get inconsistent behavior:

  • sometimes stdout is an empty string ""
  • sometimes I get this error:
    SQL1024N  A database connection does not exist.  SQLSTATE=08003
    

But I never get the actual output that I consistently receive when running the same command manually in the shell.

3
  • Db2 command shell is tricky. You cannot inherit the environment to a subprocess. Commented Nov 25 at 15:08
  • stackoverflow.com/questions/18724271/… Commented Nov 25 at 15:12
  • Both of the statements (connect, call) must run in the same subprocess for it to work correctly. You can arrange to use a single subprocess if you change the command-line to use both statements , separated by whatever character(s) your operating-system shell requires to link them (for example on unix type operating systems that is the ampersand character, while on ms-windows it is the double-ampersand). Commented Nov 26 at 10:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.