A: Why does it block?
B: How may I massage this slightly so that it will run without blocking?
#!/usr/bin/env python
import subprocess as sp
import os
kwds = dict(
stdin=sp.PIPE,
stdout=sp.PIPE,
stderr=sp.PIPE,
cwd=os.path.abspath(os.getcwd()),
shell=True,
executable='/bin/bash',
bufsize=1,
universal_newlines=True,
)
cmd = '/bin/bash'
proc = sp.Popen(cmd, **kwds)
proc.stdin.write('ls -lashtr\n')
proc.stdin.flush()
# This blocks and never returns
proc.stdout.read()
I need this to run interactively.
This is a simplified example, but the reality is I have a long running process and I'd like to startup a shell script that can more or less run arbitrary code (because it's an installation script).
EDIT: I would like to effectively take a .bash_history over several different logins, clean it up so it is a single script, and then execute the newly crafted shell script line-by-line within a shell stored within a Python script.
For example:
> ... ssh to remote aws system ...
> sudo su -
> apt-get install stuff
> su - $USERNAME
> ... create and enter a docker snapshot ...
> ... install packages, update configurations
> ... install new services, update service configurations ...
> ... drop out of snapshot ...
> ... commit the snapshot ...
> ... remove the snapshot ...
> ... update services ...
> ... restart services ...
> ... drop into a tmux within the new docker ...
This takes hours manually; it should be automated.