os.system('command')返回一个16位数字,从左开始的前8位(lsb)表示操作系统用来关闭命令的信号,接下来的8位表示命令的返回代码。00000000 00000000
exit code signal num
示例1-命令退出,代码为1os.system('command') #it returns 256
256 in 16 bits - 00000001 00000000
Exit code is 00000001 which means 1
示例2-命令退出,代码为3os.system('command') # it returns 768
768 in 16 bits - 00000011 00000000
Exit code is 00000011 which means 3
现在试试信号-
示例3-编写一个长时间睡眠的程序,在os.system()中将其用作命令,然后通过kill-15或kill-9终止它os.system('command') #it returns signal num by which it is killed
15 in bits - 00000000 00001111
Signal num is 00001111 which means 15
可以将python程序设置为command='python command.py'import sys
sys.exit(n) # here n would be exit code
对于c或c++程序,可以使用return from main()或exit(n)from any function#
注意-这适用于unixOn Unix,