Skip to main content

A Python library for managing persistent shell sessions programmatically

Project description

ShellSh

A Python library for managing persistent shell sessions programmatically with non-blocking I/O.

Features

  • Non-blocking command execution - Commands return immediately without waiting for completion
  • Command status tracking - Check if commands are still running with is_alive()
  • Flexible waiting - Wait for command completion with optional timeouts using wait()
  • Persistent shell sessions - Maintain environment variables, working directory, and state between commands
  • Real-time output streaming - Capture output from long-running processes incrementally
  • Interactive program support - Works with curses-based programs like top, vim, etc.
  • Process control - Stop running commands with Ctrl+C programmatically

Installation

pip install shellsh

Quick Start

from shellsh import ShellSh
import time

# Create a shell session
sh = ShellSh("my_session")

# Non-blocking mode (default)
sh.typeenter("ls -la")
time.sleep(0.5)
print(sh.flush())  # Get the output

# Wait for command completion
sh.typeenter("echo 'Processing...'; sleep 2; echo 'Done'")
sh.wait()  # Blocks until command finishes
print(sh.flush())

# Check if command is running
sh.typeenter("sleep 5")
print(sh.is_alive())  # True - command is running
sh.wait(2)  # Wait 2 seconds
print(sh.is_alive())  # True - still running after timeout
sh.stop()  # Stop the command

# Blocking mode - waits for command completion
sh.setblocking(True)
sh.typeenter("sleep 2; echo 'Done'")  # This will block for ~2 seconds
print(sh.flush())  # Output is ready immediately after typeenter returns

# Back to non-blocking
sh.setblocking(False)
sh.typeenter("echo 'Non-blocking'")
time.sleep(0.1)
print(sh.flush())

# Environment persists between commands
sh.typeenter("cd /tmp")
sh.typeenter("pwd")
sh.wait()
print(sh.flush())  # Prints: /tmp

# Stop long-running commands
sh.typeenter("sleep 100")
time.sleep(1)
sh.stop()  # Sends Ctrl+C

# Clean up
sh.close()

API Reference

ShellSh(name)

Create a new shell session with the given name.

typeenter(line)

Send a command to the shell. By default, returns immediately without waiting for execution. If setblocking(True) was called, blocks until the command completes.

flush()

Retrieve new output since the last flush. Returns only unread output.

wait(seconds=None)

Block until the current command completes or timeout is reached.

  • seconds=None: Wait indefinitely until command completes (default)
  • seconds=float: Maximum time to wait in seconds before returning
  • Note: Timeout does not kill the command, it just stops waiting

is_alive()

Check if a command is currently running.

  • Returns True if a command is still executing
  • Returns False if the last command has completed or no command was run

setblocking(blocking)

Set blocking mode for typeenter().

  • blocking=True: typeenter() waits for command completion before returning
  • blocking=False: typeenter() returns immediately (default)

stop()

Send Ctrl+C to stop the currently running command.

close()

Terminate the shell session and clean up resources.

Context Manager

ShellSh supports context manager protocol:

with ShellSh("session") as sh:
    sh.typeenter("echo Hello")
    time.sleep(0.5)
    print(sh.flush())
# Automatically closed

Use Cases

  • Automating shell interactions
  • Building terminal-based tools
  • Testing command-line applications
  • Managing long-running processes
  • Creating interactive shell wrappers

Requirements

  • Python 3.7+
  • Unix-like operating system (Linux, macOS)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shellsh-0.4.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shellsh-0.4.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file shellsh-0.4.0.tar.gz.

File metadata

  • Download URL: shellsh-0.4.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for shellsh-0.4.0.tar.gz
Algorithm Hash digest
SHA256 82a5f915c5600325f1dd76eafd4a086f3648d98aa8b36b6cf67020ae19fa74a9
MD5 72d9d68f356f3dcbdd1d3031935c95e3
BLAKE2b-256 8387687a14f62df1d889104ccef8710ce23218aecb66963b1845d48a6711aaa8

See more details on using hashes here.

Provenance

The following attestation bundles were made for shellsh-0.4.0.tar.gz:

Publisher: publish.yml on saturnino-adrales/shellsh

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file shellsh-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: shellsh-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for shellsh-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 39e831af78b9c1f95bf2303e5486d9785a19f21ebc0f9f0370acb243f47c45cc
MD5 1d552c54731925b87f81fbc4f960d7a2
BLAKE2b-256 aaf02ce3bb58759c67d2882226ebae2c67edfe940a50728d32161c3dc548fdd0

See more details on using hashes here.

Provenance

The following attestation bundles were made for shellsh-0.4.0-py3-none-any.whl:

Publisher: publish.yml on saturnino-adrales/shellsh

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page