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?
source to share
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).
source to share
You've pretty much mentioned everyone. A more detailed list is provided below here , and here is some useful information about each:
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).
source to share