Thread: Exit Codes

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    6
    Rep Power
    0

    Question Exit Codes


    Alright, I think my previous post about this subject was very confusing so I will try again. First off I appreciate all the responses to my prior post (Setting a Variable to the Exit Code).

    It is my understanding that when a script completes it returns an exit code. I want to capture this exit code in order to pass it to a CF page. So my questions are:

    1) Is there an exit code returned when a script completes?

    2) Is the value of the exit code stored somewhere? If so where?

    3) Can I assign the value of the exit code to a variable?
    i.e. errorlevel = exit code

    4) Can I include the variable in my URL?
    i.e. ('http://cob/gis/GISAutoProcess_Action.cfm?ProcessID=000&ErrorCode=%s' % (errorlevel))

    5) Can this be added to the end of the process or does it need to be run separately?

    6) If I need to run it separately is there a way to set a ‘global’ variable that can be called in the other process?

    Thank you everybody for your patience and understanding as I stumble my way through my first Python experience.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Originally Posted by mccarthp
    Alright, I think my previous post about this subject was very confusing so I will try again. First off I appreciate all the responses to my prior post (Setting a Variable to the Exit Code).
    Ah, I've just finished a reply to that thread. Never mind, I'll try again

    It is my understanding that when a script completes it returns an exit code.
    Yes.

    I want to capture this exit code
    In what? The only point of these error codes are to pass information to the outside world when your program has finished. So it might do:

    Code:
    ...
    
    try:
        x = run_website()
        y = mangle_database()
        z = crunch_numbers()
    except:
        #uh oh, something went wrong, I can't continue
        # Just exit and tell whoever wanted me to run that I
        # couldn't finish whatever I was doing...
        import sys;
        sys.exit(1)
    
    #Otherwise exit normally with no error code (0 by default)

    So if you are writing a script to, say, check all names in a database have an associated phone number, and then want to store the answer in the database, it doesn't make sense to:

    - write one program to check the database
    - return an error code
    - write another program to read the error code
    - and log it to the database

    when you could do it all from the first program, e.g.

    Code:
    ...
    
    try:
        x = run_website()
        y = mangle_database()
        z = crunch_numbers()
    except:
        #uh oh, something went wrong, I can't continue
        # Just log the result to the database.
        import my_db_module
        my_db_module.log_error()
    If you see what I mean.

    in order to pass it to a CF page. So my questions are:

    1) Is there an exit code returned when a script completes?
    Yes, but there is no guarantee that it will mean anything - it depends if the script author wanted to make it mean anything - most scripts will exit with 0 whether they did what they were supposed to or not, and exit with an error code if they caused a Python error - as the process that is actually running in the OS is the Python interpreter.

    Code:
    c:\> python -c ""
    c:\> echo %errorlevel%
    0
    
    c:\> python -c "1 == 2"   # silly, but valid Python, so - no error code
    c:\> echo %errorlevel%
    0
    
    c:\> python -c '1"q\'
      File "<string>", line 1
        '1q\'
            ^
    SyntaxError: EOL while scanning single-quoted string
    c:\> echo %errorlevel%
    1
    So the chances are that if you didn't write the module, the error code wont be significant or useful, and if you did write the module then you should skip all the errorlevel stuff entirely as it's a bit of a kludge.

    2) Is the value of the exit code stored somewhere? If so where?
    If the script is called from a shell (DOS/CMD/Bash/etc.) it will typically be stored by the shell. In a CMD shell in Windows, this means an environment variable that lasts until you close the shell window.

    If the script is called from another program, it will have to be done in a way that means it gets the return value, or it will just be lost.

    3) Can I assign the value of the exit code to a variable?
    i.e. errorlevel = exit code
    Yes.

    Code:
    import subprocess
    errorlevel = subprocess.call("script.py")
    or

    Code:
    import os
    errorlevel = os.getenv('errorlevel')
    # (Maybe - I'm not sure that this will ever actually work)
    4) Can I include the variable in my URL?
    i.e. ('http://cob/gis/GISAutoProcess_Action.cfm?ProcessID=000&ErrorCode=%s' % (errorlevel))
    Yes, once you have got it you can do anything you like with it. Like that.

    5) Can this be added to the end of the process or does it need to be run separately?
    This doesn't make sense.
    If you are writing the "script that will exit with an errorlevel" then skip all this completely and have it write its own success or failure to the database instead of to an errorlevel. As per my waffle above.

    6) If I need to run it separately is there a way to set a ‘global’ variable that can be called in the other process?
    There didn't used to be, which is possibly why errorlevels came about. Anything more and you could write it to a file.
    Now you could, using Named Pipes, but I have never tried them, and the chances are you don't need to.

    Why not include the other process as a module, say.
    Last edited by sfb; August 26th, 2005 at 12:54 PM.

IMN logo majestic logo threadwatch logo seochat tools logo