Skip to main content

Persistent bash sessions for Python

Project description

bashautom

Persistent bash sessions for Python.

Unlike subprocess.run() which spawns a new process every time, bashautom keeps a /bin/bash process alive so state (env vars, cwd, etc.) persists across commands.

from bashautom import Session

with Session() as s:
    s.execute("cd /opt/myproject")
    s.execute("source .env")
    s.execute("export BUILD_ID=42")
    result = s.execute("make build")

Install

pip install bashautom

Python 3.10+, Linux/macOS only.

Usage

from bashautom import Session

with Session() as s:
    result = s.execute("echo hello")
    print(result.stdout)
    print(result.exit_code)
    print(result.success)

Timeouts

Commands can be killed without destroying the session:

with Session() as s:
    result = s.execute("sleep 60", timeout=3)
    print(result.timed_out)

    # session still works
    s.execute("echo ok")

Streaming

from bashautom.session import StreamEvent

def on_output(event: StreamEvent):
    print(f"[{event.stream}] {event.data.strip()}")

with Session() as s:
    s.execute("for i in 1 2 3; do echo $i; sleep 0.5; done", stream_callback=on_output)

Multiple sessions

from bashautom import SessionManager

with SessionManager() as mgr:
    build = mgr.create("build", cwd="/opt/project")
    deploy = mgr.create("deploy", cwd="/opt/infra")

    build.execute("make release")
    deploy.execute("./deploy.sh")

Env helpers

with Session() as s:
    s.set_env("PROJECT", "bashautom")
    print(s.get_env("PROJECT"))
    print(s.get_cwd())
    print(s.pid)
    print(s.alive)

API

Session

  • execute(command, timeout=None, stream_callback=None) - run a command, returns CommandResult
  • send_signal(sig=SIGINT) - send a signal to the running process
  • get_cwd() / get_env(var) / set_env(var, value) - shell state access
  • close() - kill the session
  • pid, alive - process info

CommandResult

  • command, stdout, stderr - what ran and what came back
  • exit_code, success, timed_out - status
  • duration - wall time in seconds

SessionManager

  • create(name, ...) / get(name) / get_or_create(name, ...) - session lifecycle
  • close(name) / close_all() - cleanup
  • names, active - introspection

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

bashautom-0.1.1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

bashautom-0.1.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file bashautom-0.1.1.tar.gz.

File metadata

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

File hashes

Hashes for bashautom-0.1.1.tar.gz
Algorithm Hash digest
SHA256 785e9aa4b29106963865f03dea0d274bd9c99dac4d457dc9aa7f68d0ab39ccd3
MD5 4b929bcfb62c092e2471340316556e01
BLAKE2b-256 28125819192ef9a2bf6814e0c93cc18f47c04083aea10046079ea390aafaf0a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for bashautom-0.1.1.tar.gz:

Publisher: publish.yml on huskago/bashautom

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

File details

Details for the file bashautom-0.1.1-py3-none-any.whl.

File metadata

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

File hashes

Hashes for bashautom-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3078f43a1ee896b15d92fc734d491c8035ebb5eb09a1098000c029d6bcbddaa2
MD5 4af1878ac136f79cdbe4ea87ff65503b
BLAKE2b-256 e1bfb1a756802c9641663a3fa43d01ed0d6f607c88477709272a747c0448f59a

See more details on using hashes here.

Provenance

The following attestation bundles were made for bashautom-0.1.1-py3-none-any.whl:

Publisher: publish.yml on huskago/bashautom

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