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. ('' % (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.

Dani AI

Generated

Short answer for : yes — a finished Python process returns an integer status (0 = success, non‑zero = error). is right that the value is held by the caller (the shell or program that launched Python). It is not a “global” OS variable other unrelated processes can read; if you need other code to see it you must explicitly pass it (HTTP callback, file, DB, environment for child processes only, etc.).

A few concrete, immediate ways to capture and forward the code:

Bash wrapper (capture the shell return value and call an HTTP endpoint):

python3 /path/to/myscript.py
rc=$?
curl -G --data-urlencode "ErrorCode=$rc" "http://yourserver/notify?ProcessID=123"

Windows batch (use %ERRORLEVEL% or PowerShell to send the GET):

python C:\scripts\myscript.py
set rc=%ERRORLEVEL%
powershell -Command "Invoke-WebRequest -Uri 'http://yourserver/notify?ProcessID=123&ErrorCode=%rc%'"

In-Python (call a program and report its exit): use subprocess to get the return code, then POST/GET to your CF page:

import subprocess, requests
p = subprocess.run(['externalprog'])
rc = p.returncode
requests.get('http://yourserver/notify', params={'ProcessID':123, 'ErrorCode': rc})

Notes and troubleshooting

  • Processes do not share variables: environment variables are inherited by children but changes in a child do not affect the parent. To communicate results use a callback (HTTP), write to a file/DB, or let the caller capture the exit code and act on it.
  • Use small integer codes (0 = OK). On Unix the status is an 8‑bit value (0–255); codes >=128 often indicate termination by a signal (128 + signal number).
  • If the CF engine is launching your script, capture the exit code where the script is invoked (the caller) or have the script itself call the CF URL at the end — both are reliable options.

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

(module sys) :
exit( [arg])

Exit from Python. This is implemented by raising the SystemExit exception, so cleanup actions specified by finally clauses of try statements are honored, and it is possible to intercept the exit attempt at an outer level. The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered successful termination'' and any nonzero value is consideredabnormal termination'' by shells and the like. Most systems require it to be in the range 0-127, and produce undefined results otherwise. Some systems have a convention for assigning specific meanings to specific exit codes, but these are generally underdeveloped; Unix programs generally use 2 for command line syntax errors and 1 for all other kind of errors. If another type of object is passed, None is equivalent to passing zero, and any other object is printed to sys.stderr and results in an exit code of 1. In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.
Is the value of the exit code stored somewhere? If so where?

This integer value is stored by the context that called the process (I mean the python interpreter). This can be a shell script, or a c function (the system() function, for example)

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

No. (But I'm not sure i understand your question)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.