<@ULVA73B9P> is there a way from prefect-shell's S...
# ask-marvin
c
@Marvin is there a way from prefect-shell's ShellOperation to obtain the exit code of the command requested? I use
job.run()
and in its result does not seem to be any attribute indicating this value
m
thought for 251.4 seconds
Short answer:
job.run()
returns only the output lines. To get the exit code, use
trigger()
to get a
ShellProcess
and read its
return_code
. Example (works in both async and sync contexts):
Copy code
from prefect_shell import ShellOperation

# create the operation
op = ShellOperation(commands=["echo 'hello'", "exit 42"])

# start the process
with op as shell:
    proc = shell.trigger()  # returns a ShellProcess

    try:
        proc.wait_for_completion()  # raises RuntimeError if exit code != 0
    except RuntimeError:
        pass  # still can read the exit code

    code = proc.return_code         # <- exit code (int | None while running)
    lines = proc.fetch_result()     # output lines
    print(code, lines)
Notes: - If you stick with
run()
, a successful return implies exit code 0. On non‑zero exit,
run()
raises
RuntimeError
(the code is included in the error message), but it does not return the code directly. - With
trigger()
, you can always inspect
proc.return_code
(and
proc.pid
,
proc.fetch_result()
). References: - ShellProcess.return_code property in source: https://github.com/PrefectHQ/prefect/blob/main/src/integrations/prefect-shell/prefect_shell/commands.py#L144-L152 - wait_for_completion behavior (raises on non‑zero): https://github.com/PrefectHQ/prefect/blob/main/src/integrations/prefect-shell/prefect_shell/commands.py#L164-L183 - Integration docs: https://docs.prefect.io/integrations/prefect-shell/