简体   繁体   English

如何使用python获取脚本退出状态?

[英]how can i get script exit status with python?

I have bash script that run and return and exit code 70. I try to get the exit code with python but it will only get 0 which is different. 我有运行和返回并退出代码70的bash脚本。我尝试使用python获取退出代码,但它只会得到0,这是不同的。

my bash script 我的bash脚本

#!/bin/bash
DATE=$(date +"%Y-%m-%d_%H%M")
raspistill -vf -hf -o /home/pi/camera/$DATE.jpg

echo $?

Output 输出量

mmal: mmal_vc_component_enable: failed to enable component: ENOSPC
mmal: camera component couldn't be enabled
mmal: main: Failed to create camera component
mmal: Failed to run camera app. Please check for firmware updates

70

my python code 我的python代码

import os
import subprocess

os.chdir("/test")
result = subprocess.Popen("./test.sh")
text = result.communicate()[0]
returncode = result.returncode
print (returncode)

Output 输出量

mmal: mmal_vc_component_enable: failed to enable component: ENOSPC
mmal: camera component couldn't be enabled
mmal: main: Failed to create camera component
mmal: Failed to run camera app. Please check for firmware updates

70
0

The result of your bash script is 0. raspistill exited abnormally (exit code 70). bash脚本的结果为raspistill异常退出(退出代码70)。 The return command in bash sets the return value for your script. bash中的return命令设置脚本的返回值。

The return code is the exit status of the last command executed in the script. 返回代码是脚本中最后执行的命令的退出状态。 Since echo $? 由于echo $? ran successfully, the exit status is 0. Try the script without the echo. 运行成功,退出状态为0。尝试不带回显的脚本。

最简单的解决方案是:

exit(returncode)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM