Skip to main content

bashautom

PyPI version Python versions Downloads License: MIT Tests

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")

Why not just subprocess / pexpect?

subprocess.run() spawns a brand new shell for every call, no memory of cd, exported vars, or sourced files between commands. pexpect solves persistence but is built around interactive prompt-matching, which is overkill (and fragile) when you just want to run commands and get clean results back.

bashautom keeps one real /bin/bash process alive and gives you structured results (stdout, exit_code, success, duration) for each command, with timeouts and streaming built in, without you writing any expect-style pattern matching.

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

Release files for bashautom 0.1.2

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

Source distribution (sdist)

Source distribution for bashautom 0.1.2
File Size Uploaded
bashautom-0.1.2.tar.gz 9.4 kB Details

Built distribution (wheel)

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

Total release size:17.7 kB

Release files / bashautom-0.1.2.tar.gz

Download URL bashautom-0.1.2.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e88524441bd7e9dbc53be4a8f1296f272439b5a1f0955c598c69031af62a2d88
BLAKE2b-256 checksum
How to use checksums
bcd8ffbe70b24049ad731c82ac3020f988f78ef2f0d28a30c3b704a6560ec85c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 17, 2026.

Transparency log

Release files / bashautom-0.1.2-py3-none-any.whl

Download URL bashautom-0.1.2-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
93291d4cf75268e47064c7f3f416b19e008414e8c7941a598dbf69ce599f80c3
BLAKE2b-256 checksum
How to use checksums
18b253e3462e674ea71e524193cbeefe9d1370a2069758f7a3e1fd3b49f985a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 17, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

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