multiple commands with python subprocess
I am developing a system (college work) that consists of a site for the use of a certain board. The problem is that in order to run the code on the board, I need to run a portion of commands.
Currently my code looks like this:
def run():
comando = "cd nke/NKE0.7e/Placa && make clean && make && sudo make ispu && cd .. && sudo ./terminal64"
print(comando)
# print(os.system(comando)
process = subprocess.Popen(comando, stdout=subprocess.PIPE)
out, err = process.communicate()
print(out)
The problem is that terminal64
never ends (this is not an error), so I would need to set a time for it to run and kill it. After that (example, 2 minutes), I researched and from what I understood this can be done using the subprocess
library, but I couldn't run the same command I ran in os.system
in it.
Can anyone help me implement this command in subprocess
or set a timeout in terminal64
?
1 answers
Or popen method.communicate () has a parameter, called timeout that does exactly what you need, waits for n seconds, and in case the process has not returned, kills it and fires a TimeoutExpired exception. The code would look like this:
def run():
comando = "cd nke/NKE0.7e/Placa && make clean && make && sudo make ispu && cd .. && sudo ./terminal64"
process = subprocess.Popen(comando, stdout=subprocess.PIPE)
# espera por 2 minutos
out, err = process.communicate(timeout=120)
print(out)