Skip to main content

Overview

This library provides a utility to make executing shell commands much simpler than would be posisble using standard modules. It is built on top of the subprocess module and uses it's subprocess.Popen() function to execute a shell commands. The library allows shell commands to be executed synchronously (blocking) or asynchronously (non-blocking). It also has utility functions to collect information about the execution such as the ExitCode, the Stdout, and the Stderr.

How It Works

The Shell module provides the execute_shell_command() function as the main entry point. This fucntion accepts a number of parameters and allows the user to:

  • specify environment variables
  • specify the working directory
  • specify the shell being used
  • specify the number of retries to attempt in the event of a failure
  • specify the delay between reties
  • specify whether the execution should be blocking or non blocking

In the event of a success (zero exit code) the function will return a ShellCommandResults object. This object contains pointers to the command, the exit code, the Stdout, and the stderr.

Note: The Stdout and Stderr are strings which are decoded from the original byte stream. They will contain all the newlines that were written out to the shell.

In the event of a failure, the function will raise a ShellCommandException which extends the Exception base class. Like the ShellCommandResults object, this exception contains pointers to the command, the exit code, the Stdout, and the stderr.

Getting Started

Here is an example of the simple use case for this utility:

from ShellUtilities import Shell

shell_command_results = Shell.execute_shell_command("echo $MYVAR", env={"MYVAR": "Hello, World!"})
shell_command_results.ExitCode == 0
print(shell_command_results.StdOut) # Hello, World!
print(shell_command_results.Stderr)

And here is a more complex example of this utility running a long command asynchronously while piping the output to stdout in real time:

from ShellUtilities import Shell

# Define an async function to use when we execute the shell command
def stdout_func(stdout_line):
    print(stdout_line)
    
# Run the command and also output while the command is running
shell_command_string = r"echo 'a'; sleep 2; echo 'b'; sleep 2; echo 'c'; sleep 2; echo 'd';"
shell_command_results = Shell.execute_shell_command(shell_command_string, blocking=False, async_buffer_funcs={"stdout": [stdout_func]})
time.sleep(1)
print("hello")
shell_command_results.wait()

# The output of this main thread will be:
# a
# hello
# b
# c
# d

Change Log

2.1.15

  • Fixing bug to allow parallel execution of multiple shell commands via multiprocessing.pool.ThreadPool.
  • Adding Changelog to README.md.

Metadata

Release files for ShellUtilities 2.1.15

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ShellUtilities 2.1.15
File Size Uploaded
shellutilities-2.1.15.tar.gz 10.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ShellUtilities 2.1.15
File Interpreter ABI Platform
shellutilities-2.1.15-py3-none-any.whl Python 3 none any Details

Total release size: 18.2 kB

Release files / shellutilities-2.1.15.tar.gz

Download URL shellutilities-2.1.15.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c8a6d7e01418f850e3d541cf44c41f47b405d2b323262d0a23e396fe166ab793
BLAKE2b-256 checksum
How to use checksums
c5d246ba29239bf37e3faddfff858c4c72fa73dbce766e86fd07278f6659d7ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release files / shellutilities-2.1.15-py3-none-any.whl

Download URL shellutilities-2.1.15-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9444c13187846f9f27bbd51642f292285cf777b65698e956b8323e034f5e0f1e
BLAKE2b-256 checksum
How to use checksums
b18c850c94253f3200fd9630c3fb91b74fbf3f8ccf30026e89b402a51a9edc4d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release history Release notifications | RSS feed

This release

2.1.15 This release

2 release files

2.1.14

2 release files

2.1.12

2 release files

2.1.11

2 release files

2.1.10

2 release files

2.1.9

2 release files

2.1.8

2 release files

2.1.7

2 release files

2.1.6

2 release files

2.1.5

2 release files

2.1.4

2 release files

2.1.3

2 release files

2.1.2

2 release files

2.1.1

2 release files

2.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page