Answer the question
In order to leave comments, you need to log in
How to catch the execution code of a command sent in a python script to sh via subprocess?
having code on bash:
TIME=0
sh -c "${команда, которую я выполняю}" > /dev/null 2>&1
while test $? -ne 0 -a ${TIME} -lt ${TIMEOUT}
do
sleep 1
TIME=`expr ${TIME} + 1`
sh -c "${команда, которую я выполняю}" > /dev/null 2>&1
done
return $?
T=0
subprocess.run(['sh', 'команда, которую я выполняю'], stdout=subprocess.PIPE)
Answer the question
In order to leave comments, you need to log in
returncode = subprocess.run(['sh', 'команда, которую я выполняю'], stdout=subprocess.PIPE).returncode
print(returncode)
Here is what you need
import subprocess
p = subprocess.Popen('sh ', shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate('echo "Hello!"'.encode())
print(out.decode('cp866'),err.decode('cp866'))
p.wait()
input()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question