Interview Prep/Python for DevOps/Runtime, file, subprocess & encoding errors

`subprocess.TimeoutExpired: Command '['ssh', 'web1', 'uptime']' timed out after 30 seconds`, and the script hangs anyway.

AdvancedTroubleshootingRuntime, file, subprocess & encoding errors
Answer

Likely causes → subprocess.run(timeout=...) kills the direct child, but grandchildren (an SSH ControlMaster, a shell's children) keep the pipes open, so reading output blocks.

Diagnose →

ps -ef --forest | grep -A3 ssh

Fix → Start the child in its own process group (start_new_session=True) and on timeout kill the whole group with os.killpg(proc.pid, signal.SIGKILL); add -o ConnectTimeout=5 -o BatchMode=yes to SSH so it cannot wait for a password prompt.

Prevent → Non-interactive flags on every remote command and process-group cleanup in your run() helper.

All Python for DevOps questions Roadmap: Python for automation

More on Runtime, file, subprocess & encoding errors