Called process returned non-zero exit code
Production Risk
Always capture and log stderr; never use shell=True with untrusted input.
Raised by subprocess.check_call() and subprocess.check_output() when the child process returns a non-zero exit code. Contains the exit code, command, and stderr output.
- 1subprocess.run(..., check=True) and the command fails
- 2subprocess.check_output() on a command that returns non-zero
- 3The external command encounters an error (missing file, wrong arguments, etc.)
Running a command that fails with check=True.
import subprocess subprocess.run(['ls', '/nonexistent'], check=True, capture_output=True)
expected output
subprocess.CalledProcessError: Command '['ls', '/nonexistent']' returned non-zero exit status 2
Fix
Catch CalledProcessError and inspect stderr
WHEN Running external commands that might fail
import subprocess
try:
result = subprocess.run(
['mycommand', 'arg'],
check=True,
capture_output=True,
text=True
)
print(result.stdout)
except subprocess.CalledProcessError as e:
print(f"Command failed with exit {e.returncode}", file=sys.stderr)
print(f"stderr: {e.stderr}", file=sys.stderr)
raiseWhy this works
e.returncode, e.stdout, and e.stderr contain the full context of the failure.
import subprocess subprocess.run(["ls", "/nonexistent"], check=True) # CalledProcessError
import subprocess
try:
subprocess.run(cmd, check=True, capture_output=True)
except subprocess.CalledProcessError as e:
print(f"Exit {e.returncode}: {e.stderr.decode()}")result = subprocess.run(cmd, capture_output=True)
if result.returncode != 0:
print(f"Command failed: {result.stderr.decode()}")✕ Use shell=True with user input
shell=True passes the command to /bin/sh — user input can inject arbitrary shell commands (shell injection vulnerability).
Python Docs — subprocess module
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev