Reputation: 2172
I am trying to run a set of bash script commands in a python program. I have to run the commands one by one and handle errors and exceptions for each command. For this purpose, I am using the subprocess
module with the call
function as bellow:
result = subprocess.call("echo testing", shell = True)
as expected this command prints "testing" and sets the value of result
to 0, meaning that the command was successfully executed. Or, in the case of following command:
result = subprocess.call("echso testing", shell = True)
it prints "/bin/sh: 1: echso: not found" and sets the value of result
to 127, meaning that the command echso
is invalid.
My qusetion is, where can I find a complete list of these error numbers with descriptions that I could use for error handling? So far, I found a list of exit errors as follows:
1: general errors
2: misuse of shell builtins (pretty rare)
126: cannot invoke requested command
127: command not found error
128: invalid argument to “exit”
128+n: fatal error signal “n” (for example, kill -9 = 137)
130: script terminated by Ctrl-C
Is this all, or do you know more error codes with descriptions?
Upvotes: 4
Views: 4738
Reputation: 2206
You pretty much mentioned all of them. A more elaborate list is given here, and here's some useful info about each of them:
According to the above table, exit codes 1 - 2, 126 - 165, and 255 have special meanings, and should therefore be avoided for user-specified exit parameters. Ending a script with exit 127 would certainly cause confusion when troubleshooting (is the error code a "command not found" or a user-defined one?). However, many scripts use an exit 1 as a general bailout-upon-error. Since exit code 1 signifies so many possible errors, it is not particularly useful in debugging.
There has been an attempt to systematize exit status numbers (see /usr/include/sysexits.h), but this is intended for C and C++ programmers. A similar standard for scripting might be appropriate. The author of this document proposes restricting user-defined exit codes to the range 64 - 113 (in addition to 0, for success), to conform with the C/C++ standard. This would allot 50 valid codes, and make troubleshooting scripts more straightforward.
Out of range exit values can result in unexpected exit codes. An exit value greater than 255 returns an exit code modulo 256. For example, exit 3809 gives an exit code of 225 (3809 % 256 = 225).
Upvotes: 1
Reputation: 67968
result = subprocess.Popen("echo testing", shell = True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output,err=result.communicate()
if output:
print "success"
else:
print err
Instead of finding errors to numbers you can find errors directly and handle them.
Upvotes: 1
Reputation: 80931
The bash man page has a section 3.7.5 Exit Status which covers the values you indicated (though I don't see 130 in there).
Beyond that I don't know that there is any good standard.
There is sysexits.h but I'm not sure how much anyone actually uses it (available on linux also).
Upvotes: 0