Last active
August 29, 2015 14:14
-
-
Save integrii/9856eb7fe1db0e0bb417 to your computer and use it in GitHub Desktop.
Get output of non-blocking bash commands with python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Be sure to run this as root! | |
import subprocess | |
def runcmd(command, timeout): | |
command = command + " & pid=$!;sleep " + str(timeout) + "; kill -9 $pid" | |
#print(command) | |
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) | |
return process.stdout.read() | |
result = runcmd('hcidump', 5) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It wasn't easy trying to get python to run a bash command for a limited time AND give me the output of it. I found a lot of custom modules and solutions for this, but prefer this hack I came up with that time-boxes the execution at the shell instead of within Python.