Here\'s the Python code to run an arbitrary command returning its stdout data, or raise an exception on non-zero exit codes:
stdout
proc = subprocess.P
surprised nobody mentioned using timeout
timeout
timeout 5 ping -c 3 somehost
This won't for work for every use case obviously, but if your dealing with a simple script, this is hard to beat.
Also available as gtimeout in coreutils via homebrew for mac users.
homebrew