$的退出状态?发生分段错误时使用 python
Exit status of $? using python when segmentation fault occured
我需要使用 python3 执行 echo $?
并捕获退出状态。我特别需要这个来捕获 Segmentation fault (core dumped)
状态。
我试过了:
>>> os.system('echo $?')
0
0
得到了0 0
。另外,对于段错误,
>>> os.system('./a.out')
Segmentation fault (core dumped)
35584
经过上面的命令,我又得到了:
>>> os.system('echo $?')
0
0
此外,为什么 0
被打印了两次?
我浏览了 python-3 的文档,上面写着:
os.system(command)
On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.
这是否说明了这种行为?
帮我澄清一下。
注意: 在上述所有步骤之前,我已经 运行 ulimit -c unlimited
。预期结果应为非零或 139(具体而言)。
编辑:我在想这个有没有限制!
谢谢!
当运行时:
>>> os.system('echo $?')
0
0
如果您之前的命令成功,第一个 0
将由 echo $?
打印,另一个将是调用 echo $?
的 return 代码刚刚成功,所以您将打印另一个 0
。
您执行的 script/command 的 return 代码将通过 os.system 函数直接 return 编辑到您的 python 程序中,因此您不需要需要使用 echo $?
示例:
$ more return_code*
::::::::::::::
return_code1.py
::::::::::::::
import os
print os.system('sleep 1')
#will print 0 after 1sec
::::::::::::::
return_code2.py
::::::::::::::
import os
print os.system('ls abcdef')
#will print a rc!=0 if the file abcdef is not present in your working directory
执行次数:
$ python return_code1.py
0
和
$ python return_code2.py
ls: cannot access 'abcdef': No such file or directory
512
不,您不需要执行 echo $?
。这不会有用的。程序的退出状态是函数os.system
的return值。这就是数字 35584
的意思。解释
的documentation os os.system
tells you to read the documentation of os.wait
a 16-bit number, whose low byte is the signal number that killed the process, and whose high byte is the exit status (if the signal number is zero); the high bit of the low byte is set if a core file was produced.
但是,请注意,根据 shell,使用 os.system('./a.out')
,您可能会获得 a.out
的退出状态或 shell 本身的退出状态.通常没有区别,因为 shell 的退出状态是它执行的最后一个命令的退出状态。但是如果命令死于信号,那就有区别了。 shell 不会用相同的信号杀死自己,它会 return 编码信号的状态。在大多数 shell 中,那是 128 + signal_number
。例如,如果程序死于信号 11(Linux 上的段错误)并留下核心转储,那么它的状态为 returned by wait
是 11。但是如果有 shell 之间,则 shell 将正常退出,退出代码为 128+11。这就是您所看到的:35584 是 (128 + 11) << 8
.
为避免这种复杂情况,请使用 subprocess.call
或其变体之一(如果您不需要代码 运行 Python <,则可以使用 subprocess.run
=3.4).
returncode = subprocess.call(['./a.out'], shell=False).returncode
if returncode & 0xff == 0:
exit_code = returncode >> 8
print('The program exited normally with status {}.'.format(exit_code))
else:
print('The program was killed by signal {}.'.format(returncode))
如果你 运行 os.system('echo $?')
,这将启动一个新的 shell。在 运行 任何命令之前,您正在那个 shell 中打印 $?
的初始值,而 shell 中的 $?
的初始值是0.
你在交互式环境中看到了两次 0
,因为第一个是 echo
命令打印的,第二个是 Python 表达式的值。比较 os.system('echo hello')
.
请注意,使用 os.system
,您无法访问命令的输出,因此如果您使用 echo
打印某些内容,则无法在程序中使用它。为此,您必须使用 subprocess
module 中的函数,但只有在需要 ./a.out
的输出时才需要它,而不是获取其退出状态。
我针对上述问题编写了以下代码,并按预期工作。我使用 subprocess.Popen()
方法来实现我的要求。我使用 sample.returncode
获取 shell.
的退出状态
def run_cmd():
ret = 0
sample_cmd = "./a.out"
sample = subprocess.Popen(sample_cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out_stdout, out_stderr = sample.communicate()
if sample.returncode != 0:
print ("OUTPUT: %s\nERROR: %s\n"%(out_stdout, out_stderr))
print ("Command: %s \nStatus: FAIL "%(sample_cmd))
sys.stdout.flush()
if sample.returncode == 139:
print('Segmentation fauilt(core dumped) occured...with status: ', sample.returncode)
ret = sample.returncode
else:
ret = 1
else:
print ("OUTPUT: %s\n"%(out_stdout))
print ("Command: %s \nStatus: PASS "%(sample_cmd))
ret = 0
我需要使用 python3 执行 echo $?
并捕获退出状态。我特别需要这个来捕获 Segmentation fault (core dumped)
状态。
我试过了:
>>> os.system('echo $?')
0
0
得到了0 0
。另外,对于段错误,
>>> os.system('./a.out')
Segmentation fault (core dumped)
35584
经过上面的命令,我又得到了:
>>> os.system('echo $?')
0
0
此外,为什么 0
被打印了两次?
我浏览了 python-3 的文档,上面写着:
os.system(command)
On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.
这是否说明了这种行为? 帮我澄清一下。
注意: 在上述所有步骤之前,我已经 运行 ulimit -c unlimited
。预期结果应为非零或 139(具体而言)。
编辑:我在想这个有没有限制!
谢谢!
当运行时:
>>> os.system('echo $?')
0
0
如果您之前的命令成功,第一个 0
将由 echo $?
打印,另一个将是调用 echo $?
的 return 代码刚刚成功,所以您将打印另一个 0
。
您执行的 script/command 的 return 代码将通过 os.system 函数直接 return 编辑到您的 python 程序中,因此您不需要需要使用 echo $?
示例:
$ more return_code*
::::::::::::::
return_code1.py
::::::::::::::
import os
print os.system('sleep 1')
#will print 0 after 1sec
::::::::::::::
return_code2.py
::::::::::::::
import os
print os.system('ls abcdef')
#will print a rc!=0 if the file abcdef is not present in your working directory
执行次数:
$ python return_code1.py
0
和
$ python return_code2.py
ls: cannot access 'abcdef': No such file or directory
512
不,您不需要执行 echo $?
。这不会有用的。程序的退出状态是函数os.system
的return值。这就是数字 35584
的意思。解释
os.system
tells you to read the documentation of os.wait
a 16-bit number, whose low byte is the signal number that killed the process, and whose high byte is the exit status (if the signal number is zero); the high bit of the low byte is set if a core file was produced.
但是,请注意,根据 shell,使用 os.system('./a.out')
,您可能会获得 a.out
的退出状态或 shell 本身的退出状态.通常没有区别,因为 shell 的退出状态是它执行的最后一个命令的退出状态。但是如果命令死于信号,那就有区别了。 shell 不会用相同的信号杀死自己,它会 return 编码信号的状态。在大多数 shell 中,那是 128 + signal_number
。例如,如果程序死于信号 11(Linux 上的段错误)并留下核心转储,那么它的状态为 returned by wait
是 11。但是如果有 shell 之间,则 shell 将正常退出,退出代码为 128+11。这就是您所看到的:35584 是 (128 + 11) << 8
.
为避免这种复杂情况,请使用 subprocess.call
或其变体之一(如果您不需要代码 运行 Python <,则可以使用 subprocess.run
=3.4).
returncode = subprocess.call(['./a.out'], shell=False).returncode
if returncode & 0xff == 0:
exit_code = returncode >> 8
print('The program exited normally with status {}.'.format(exit_code))
else:
print('The program was killed by signal {}.'.format(returncode))
如果你 运行 os.system('echo $?')
,这将启动一个新的 shell。在 运行 任何命令之前,您正在那个 shell 中打印 $?
的初始值,而 shell 中的 $?
的初始值是0.
你在交互式环境中看到了两次 0
,因为第一个是 echo
命令打印的,第二个是 Python 表达式的值。比较 os.system('echo hello')
.
请注意,使用 os.system
,您无法访问命令的输出,因此如果您使用 echo
打印某些内容,则无法在程序中使用它。为此,您必须使用 subprocess
module 中的函数,但只有在需要 ./a.out
的输出时才需要它,而不是获取其退出状态。
我针对上述问题编写了以下代码,并按预期工作。我使用 subprocess.Popen()
方法来实现我的要求。我使用 sample.returncode
获取 shell.
def run_cmd():
ret = 0
sample_cmd = "./a.out"
sample = subprocess.Popen(sample_cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out_stdout, out_stderr = sample.communicate()
if sample.returncode != 0:
print ("OUTPUT: %s\nERROR: %s\n"%(out_stdout, out_stderr))
print ("Command: %s \nStatus: FAIL "%(sample_cmd))
sys.stdout.flush()
if sample.returncode == 139:
print('Segmentation fauilt(core dumped) occured...with status: ', sample.returncode)
ret = sample.returncode
else:
ret = 1
else:
print ("OUTPUT: %s\n"%(out_stdout))
print ("Command: %s \nStatus: PASS "%(sample_cmd))
ret = 0