The return value of Os.system () in Python

Source: Internet
Author: User
Tags connection reset

Recently encountered Os.system () execution System command, the Internet collected a bit of information, organized as follows, in case of a rainy days, but also hope to help some people.

The relationship between the return value of Os.system (cmd) in Python and the return value of the Linux command ( see additional content in this article )

Everyone is accustomed to using the OS.SYSTEMV () function to execute the Linux command, which returns a decimal number (corresponding to a 16-bit binary number). The return value of the function is converted to the return value of the Linux command: The function's return value (decimal) is converted to 162 binary number, intercept its high eight bits (if the low number is 0, the operating system error code total 131, so the low is 0), and then the number of decimal is The Linux command returns a value of 0.

For example:

The return value of the Os.system () 0 Linux command returns a value of 0.

Os.system () The return value is 256, 16 bits binary number is: 00000001,00000000, high eight bit interchange decimal 1 corresponding to the Linux command return value 1

Os.system () The return value is 512, 16 bits binary number is: 00000010,00000000, high eight bit interchange decimal 2 corresponding to the Linux command return value 2

...... Other empathy

Os.system () The return value is 32512, 16 bits binary number is: 01111111,00000000, high eight bit interchange decimal 127 corresponding to the Linux command return value 127

........

/************************************************************************************************************** ********/

Problem:/bin/xxx.py is a program with a return code of 1. When the Python program uses Os.system ("./bin/xxx.py") to call, the return value of Os.system after a successful run has a problem and becomes 256, which is 0x100. Instead of 1 that should normally be returned.


Resolved: The lookup of document Discovery Os.system () returned as:
on Unix, the return value is the exit status of the process encoded in the format speci Fied for Wait ().
and Os.wait () is returned as:
a 16-bit number, whose low byte was the signal number that killed the process, and whose high byte I s the exit status (if the signal number
is zero);
The return value of Os.system is not the return result of the executing program. Instead of a 16-bit number, its high position is the return code. That is, Os.system () returns 256 that is 0x0100, and the return code should be its high-level 0x01, or 1. So to get the value of the program running out (like return 0 in the main function of C), you need to deal with it.

ret = Os.system ('./a.out ')

RET >>= 8

This will get the correct return value. Also note that Python gets a value that is an unsigned integer, so when a negative number is returned, the print is a large positive. For example, return -1,python will get to 255,-2 254, and so on. So it is best to judge whether it is 0, I really want to judge their own C program return value, it is recommended to return 0,1,2,3 equivalent, error return-1.

In addition, I encountered once clearly handled the return value, c program debugging information hint also return the value 0, the result python gets to 1, and no matter how much the C program returns, Python gets-1. Later troubleshooting the C program, found that the original is because I this Python program is called by another C program, and call it in the C program to ignore the SIGCLD signal (this indicates that Python is based on the process of processing the signal generated when the child processes to get the return value), I bind the SIGCLD of the C program to the function, and even if that function does nothing, Python can get the correct return value.

/************************************************************************************************************** ********/

After the Linux command executes, there is a return value whether or not it is successful:

If 0, the command executes successfully, other values indicate errors, and the specific error code has the following meanings:

"OS error code 1:operation not permitted"
"OS error code 2:no such file or directory"
"OS error code 3:no such process"
"OS error code 4:interrupted system call"
"OS error code 5:input/output error"
"OS error code 6:no such device or address"
"OS error code 7:argument list too long"
"OS error code 8:exec format error"
"OS error code 9:bad file descriptor"
"OS error code 10:no child processes"
"OS error code 11:resource temporarily unavailable"
"OS error code 12:cannot allocate memory"
"OS error code 13:permission denied"
"OS error code 14:bad address"
"OS error code 15:block device required"
"OS error code 16:device or resource Busy"
"OS error code 17:file exists"
"OS error code 18:invalid cross-device link"
"OS error code 19:no such device"
"OS error code 20:not a directory"
"OS error code 21:is a directory"
"OS error code 22:invalid argument"
"OS error code 23:too many open files in system"
"OS error code 24:too many open files"
"OS error code 25:inappropriate IOCTL for device"
"OS error code 26:text file Busy"
"OS error code 27:file too large"
"OS error code 28:no space left on device"
"OS error code 29:illegal seek"
"OS error code 30:read-only file System"
"OS error code 31:too many links"
"OS Error code 32:broken pipe"
"OS error code 33:numerical argument out of domain"
"OS error code 34:numerical result out of range"
"OS error code 35:resource deadlock avoided"
"OS error code 36:file name too long"
"OS error code 37:no locks available"
"OS error code 38:function not implemented"
"OS error code 39:directory not empty"
"OS error code 40:too Many levels of symbolic links"
"OS error code 42:no message of desired type"
"OS error code 43:identifier removed"
"OS error code 44:channel number out of range"
"OS error code 45:level 2 not synchronized"
"OS error code 46:level 3 halted"
"OS error code 47:level 3 Reset"
"OS error code 48:link number out of range"
"OS error code 49:protocol driver not attached"
"OS error code 50:NO CSI structure available"
"OS error code 51:level 2 halted"
"OS error code 52:invalid Exchange"
"OS error code 53:invalid request Descriptor"
"OS error code 54:exchange full"
"OS error code 55:no anode"
"OS error code 56:invalid request Code"
"OS error code 57:invalid slot"
"OS error code 59:bad font file format"
"OS error code 60:device not a stream"
"OS error code 61:no data available"
"OS error code 62:timer expired"
"OS error code 63:out of streams resources"
"OS error code 64:machine is not on the network"
"OS error code 65:package not installed"
"OS error code 66:object is remote"
"OS error code 67:link has been severed"
"OS error code 68:advertise error"
"OS error code 69:srmount error"
"OS error code 70:communication error on send"
"OS error code 71:protocol error"
"OS error code 72:multihop attempted"
"OS error code 73:rfs specific error"
"OS error code 74:bad message"
"OS error code 75:value too large for defined data type"
"OS error code 76:name not unique on network"
"OS error code 77:file descriptor in Bad state"
"OS error code 78:remote address changed"
"OS error code 79:can not access a needed shared library"
"OS error code 80:accessing a corrupted shared library"
"OS error code Bayi:. lib section in a.out corrupted"
"OS error code 82:attempting to link in too many shared libraries"
"OS error code 83:cannot exec a shared library directly"
"OS error code 84:invalid or incomplete multibyte or wide character"
"OS error code 85:interrupted system call should is restarted"
"OS Error code 86:streams pipe Error"
"OS error code 87:too many users"
"OS error code 88:socket operation on Non-socket"
"OS error code 89:destination address required"
"OS error code 90:message too long"
"OS error code 91:protocol wrong type for socket"
"OS error code 92:protocol not available"
"OS error code 93:protocol not supported"
"OS error code 94:socket type not supported"
"OS error code 95:operation not supported"
"OS error code 96:protocol family not supported"
"OS error code 97:address family not supported by protocol"
"OS error code 98:address already in use"
"OS error code 99:cannot assign requested address"
"OS error code 100:network is down"
"OS error code 101:network is unreachable"
"OS error code 102:network dropped connection on reset"
"OS error code 103:software caused connection abort"
"OS error code 104:connection reset by Peer"
"OS error code 105:no buffer space available"
"OS error code 106:transport endpoint is already connected"
"OS error code 107:transport endpoint is not connected"
"OS error code 108:cannot send after transport endpoint shutdown"
"OS error code 109:too many references:cannot splice"
"OS error code 110:connection timed out"
"OS error code 111:connection refused"
"OS error code 112:host is down"
"OS error code 113:no route to host"
"OS error code 114:operation already in progress"
"OS error code 115:operation now in progress"
"OS error code 116:stale NFS file Handle"
"OS error code 117:structure needs Cleaning"
"OS error code 118:not a xenix named type file"
"OS error code 119:no XENIX semaphores available"
"OS error code 120:is a named type file"
"OS error code 121:remote I/O error"
"OS error code 122:disk quota exceeded"
"OS error code 123:no medium found"
"OS error code 124:wrong medium Type"
"OS error code 125:operation canceled"
"OS error code 126:required key not available"
"OS error code 127:key has expired"
"OS error code 128:key has been revoked"
"OS error code 129:key is rejected by service"
"OS error code 130:owner died"
"OS error code 131:state not recoverable"


The return value of Os.system () in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.