Skip to main content

prefect_shell.commands

Tasks for interacting with shell commands

Functions

shell_run_command

Runs arbitrary shell commands. Args:
  • command: Shell command to be executed; can also be provided post-initialization by calling this task instance.
  • env: Dictionary of environment variables to use for the subprocess; can also be provided at runtime.
  • helper_command: String representing a shell command, which will be executed prior to the command in the same process. Can be used to change directories, define helper functions, etc. for different commands in a flow.
  • shell: Shell to run the command with.
  • extension: File extension to be appended to the command to be executed.
  • return_all: Whether this task should return all lines of stdout as a list, or just the last line as a string.
  • stream_level: The logging level of the stream; defaults to 20 equivalent to logging.INFO.
  • cwd: The working directory context the command will be executed within
Returns:
  • If return all, returns all lines as a list; else the last line as a string.

Classes

ShellProcess

A class representing a shell process. Supports both async (anyio.abc.Process) and sync (subprocess.Popen) processes. Methods:

afetch_result

Retrieve the output of the shell operation (async version). Returns:
  • The lines output from the shell operation as a list.

await_for_completion

Wait for the shell command to complete after a process is triggered (async version).

fetch_result

Retrieve the output of the shell operation (sync version). Returns:
  • The lines output from the shell operation as a list.

pid

The PID of the process. Returns:
  • The PID of the process.

return_code

The return code of the process. Returns:
  • The return code of the process, or None if the process is still running.

wait_for_completion

Wait for the shell command to complete after a process is triggered (sync version).

ShellOperation

A block representing a shell operation, containing multiple commands. For long-lasting operations, use the trigger method and utilize the block as a context manager for automatic closure of processes when context is exited. If not, manually call the close method to close processes. For short-lasting operations, use the run method. Context is automatically managed with this method. Attributes:
  • commands: A list of commands to execute sequentially.
  • stream_output: Whether to stream output.
  • env: A dictionary of environment variables to set for the shell operation.
  • working_dir: The working directory context the commands will be executed within.
  • shell: The shell to use to execute the commands.
  • extension: The extension to use for the temporary file. if unset defaults to .ps1 on Windows and .sh on other platforms.
Examples: Load a configured block:
Methods:

aclose

Close the job block (async version).

arun

Runs a shell command (async version), but unlike the trigger method, additionally waits and fetches the result directly, automatically managing the context. This method is ideal for short-lasting shell commands; for long-lasting shell commands, it is recommended to use the trigger method instead. Args:
  • **open_kwargs: Additional keyword arguments to pass to open_process.
Returns:
  • The lines output from the shell command as a list.
Examples: Sleep for 5 seconds and then print “Hello, world!”:

atrigger

Triggers a shell command and returns the shell command run object to track the execution of the run (async version). This method is ideal for long-lasting shell commands; for short-lasting shell commands, it is recommended to use the run method instead. Args:
  • **open_kwargs: Additional keyword arguments to pass to open_process.
Returns:
  • A ShellProcess object.
Examples: Sleep for 5 seconds and then print “Hello, world!”:

close

Close the job block (sync version).

run

Runs a shell command (sync version), but unlike the trigger method, additionally waits and fetches the result directly, automatically managing the context. This method is ideal for short-lasting shell commands; for long-lasting shell commands, it is recommended to use the trigger method instead. Args:
  • **open_kwargs: Additional keyword arguments to pass to subprocess.Popen.
Returns:
  • The lines output from the shell command as a list.
Examples: Sleep for 5 seconds and then print “Hello, world!”:

trigger

Triggers a shell command and returns the shell command run object to track the execution of the run (sync version). This method is ideal for long-lasting shell commands; for short-lasting shell commands, it is recommended to use the run method instead. Args:
  • **open_kwargs: Additional keyword arguments to pass to subprocess.Popen.
Returns:
  • A ShellProcess object.
Examples: Sleep for 5 seconds and then print “Hello, world!”: