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")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.
pip install bashautomPython 3.10+, Linux/macOS only.
from bashautom import Session
with Session() as s:
result = s.execute("echo hello")
print(result.stdout)
print(result.exit_code)
print(result.success)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")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)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")with Session() as s:
s.set_env("PROJECT", "bashautom")
print(s.get_env("PROJECT"))
print(s.get_cwd())
print(s.pid)
print(s.alive)execute(command, timeout=None, stream_callback=None)- run a command, returnsCommandResultsend_signal(sig=SIGINT)- send a signal to the running processget_cwd()/get_env(var)/set_env(var, value)- shell state accessclose()- kill the sessionpid,alive- process info
command,stdout,stderr- what ran and what came backexit_code,success,timed_out- statusduration- wall time in seconds
create(name, ...)/get(name)/get_or_create(name, ...)- session lifecycleclose(name)/close_all()- cleanupnames,active- introspection
MIT