Search code examples
pythonerror-handlingexit-code

Python script returns code 244 despite different sys.exit() code


I am trying to pass a controlled error code out of a script which calls multiple binaries using os.system. However, despite checking for error code returned by os.system and catching exceptions, I get the code 244 out in case any of the binaries fail to run.

import os, sys

try:
    error_code = os.system('mdkir')
    if error_code != 0:
        sys.exit(500) # Reaches here when running in Debug mode, as expected
    else:
        sys.exit(200)
except:
    sys.exit(500)

Process finished with exit code 244 # Why not 500? 

Please suggest how to do this properly. This script will be called by a flask-based web interface.


Solution

  • Exit codes in Linux are 1 byte or 8 bits.

    500 is 0b111110100 that's 9 bits. The leading 1 gets trimmed to fit into 8 bits.

    We're left with 0b11110100. And that's 244.