4

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?

Bharadwaj Srigiriraju
  • 2,196
  • 4
  • 25
  • 45
user823743
  • 2,152
  • 3
  • 21
  • 31
  • 1
    That list you have is about as good as it gets. Each program is free to use whatever exit codes it wants. You really can't rely on anything besides "nonzero equals error." That said, you can capture `stderr` and offer to present that to the user. – kindall Dec 05 '14 at 14:42
  • 1
    Possible duplicate of http://stackoverflow.com/questions/1101957/are-there-any-standard-exit-status-codes-in-linux – runDOSrun Dec 05 '14 at 14:45

3 Answers3

1
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.

vks
  • 67,027
  • 10
  • 91
  • 124
  • 1
    This isn't an answer to the question. – Etan Reisner Dec 05 '14 at 14:40
  • How does this answer the question of where he can find a list of exit coder for the shell? – RedX Dec 05 '14 at 14:40
  • This gives you standard error from the run command (I believe) that's not always the same thing. – Etan Reisner Dec 05 '14 at 14:43
  • @EtanReisner he basically wants to handle errors.I guess this is a fine way to that – vks Dec 05 '14 at 14:45
  • Except that isn't the question he actually asked and this covers a slightly different case. But yes, in general this is a reasonable alternative suggestion but that should be more clearly explained and with the reasons why. – Etan Reisner Dec 05 '14 at 14:46
  • @vks thanks for the answer. your code results in this error: Traceback (most recent call last): File "/home/a02/workspace/test/main.py", line 7, in result = subprocess.Popen("echo testing", shell = True,stdout=subprocess.PIPE,stderr=subprocess.Popen) File "/usr/lib/python2.7/subprocess.py", line 672, in __init__ errread, errwrite) = self._get_handles(stdin, stdout, stderr) File "/usr/lib/python2.7/subprocess.py", line 1065, in _get_handles errwrite = stderr.fileno() AttributeError: type object 'Popen' has no attribute 'fileno' Am I missing something here? – user823743 Dec 05 '14 at 14:55
  • @user823743 Typo `stderr=subprocess.Popen` should be `stderr=subprocess.PIPE`. – Etan Reisner Dec 05 '14 at 15:02
1

You pretty much mentioned all of them. A more elaborate list is given here, and here's some useful info about each of them:

reserved exit codes

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).

Bharadwaj Srigiriraju
  • 2,196
  • 4
  • 25
  • 45
0

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).

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • That is why i prefer the other solution.It at least handles all the cases – vks Dec 05 '14 at 14:51
  • @vks No, it doesn't handle all the cases. It doesn't handle things that don't write to stderr but return a failure return code. – Etan Reisner Dec 05 '14 at 15:02