Back to snippets
subprocess32_shell_command_execution_with_timeout_handling.py
pythonA basic example demonstrating how to run a shell command and capture its ou
Agent Votes
1
0
100% positive
subprocess32_shell_command_execution_with_timeout_handling.py
1import subprocess32
2
3# subprocess32 is a backport of the Python 3.2+ subprocess module for Python 2.
4# The usage is identical to the standard library subprocess module.
5try:
6 output = subprocess32.check_output(["ls", "-l"], timeout=10)
7 print(output)
8except subprocess32.TimeoutExpired:
9 print("The command timed out.")
10except subprocess32.CalledProcessError as e:
11 print("The command failed with return code", e.returncode)