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.