Executing bash commands in python and handling errors

I am trying to run a bash script command set 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 a module subprocess

with a function call

as shown below:

result = subprocess.call("echo testing", shell = True)

      

as expected, this command prints "test" and sets the value result

to 0, which means the command was completed successfully. Or, in the case of the following command:

result = subprocess.call("echso testing", shell = True)

      

it prints "/ bin / sh: 1: echso: not found" and sets the value result

to 127 which means the command is echso

invalid. My qusetion is: where can I find a complete list of these error numbers with descriptions that I could use to handle errors? So far I have found the logout error list 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 that all, or do you know more error codes with descriptions?

+3


source to share


3 answers


There is a section 3.7.5 Exit Status in the bash manpage that covers the values ​​you specified (although I don't see 130 there).

Also, I don't know there is a good standard.



There is sysexits.h , but I'm not sure how much actually uses it (also available on Linux).

0


source


You've pretty much mentioned everyone. A more detailed list is provided below here , and here is some useful information about each:

reserved exit codes



According to the table above, exit codes 1 - 2, 126 - 165 and 255 have special meanings and should therefore be avoided for user-defined exit parameters. Ending a script with output 127 will certainly cause confusion when troubleshooting (is it a "command not found" error code or a custom one?). However, many scripts use output 1 as a general fallback on error. Since exit code 1 means so many possible errors, this is not particularly useful when debugging.

There was 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 suggests limiting custom output codes to the range 64 - 113 (in addition to 0, for success), up to the C / C ++ standard. This will contain 50 valid codes and make troubleshooting scripts easier.

Out of range exit values ​​can lead to unexpected exit codes. An exit greater than 255 returns an exit code modulo 256. For example, exit 3809 gives an exit code of 225 (3809% 256 = 225).

0


source


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 in numbers, you can find errors directly and handle them.

-1


source







All Articles