Daemux

Daemux lets you run daemons in a tmux pane.

That way, you wan write programs that launch long-running background tasks, and check these tasks’ health by hand, relaunch them, etc. by attaching to the corresponding pane in tmux.

>>> import daemux
>>> # session, window, and pane are implicitely deduced if
>>> # not explicitely specified
>>> yes = daemux.start('yes')
>>> yes.status()
'running'
>>> # One can reattach from somewhere else
>>> yes2 = daemux.reattach(session='yes', window='yes', pane=-1)
>>> yes2.status()
'running'
>>> # Reattaching gives full control
>>> yes2.stop()
>>> yes2.status()
'ready'
>>> # Control is still available from the original instance
>>> yes.status()
'ready'
>>> yes.start()
>>> yes2.status()
'running'
>>> yes.stop()
class daemux.Daemon(cmd, session=None, window=None, pane=None, layout=None)[source]

Handle tmux session, window and pane to control the daemon.

__init__(cmd, session=None, window=None, pane=None, layout=None)[source]

Create or attach to a session/window/pane for command cmd.

Args:

cmd: The command to run to start the daemon.

session: The name of the tmux session in which to
run the daemon. Derived from cmd if None. Will be created if it does not already exists.
window: The name of the tmux window (inside of session)
in which to run the daemon. Derived frm cmd if None. Will be created if it does not already exists.
pane: The number of the pane (inside of window) in which
to run the daemon. A new pane will be created if None. As many panes as necessary will be created so that pane number pane exists. Python indexes work, so asking for pane e.g. -1 makes sense.
layout: The layout to apply after each pane creation. Defaults
to None, in which case no layout is applied. Creating too many panes will eventually make tmux fail, complaining that there is not enough space left to create a new pane. Using the e.g. ‘tiled’ layout is a good way to delay this problem.
__weakref__

list of weak references to the object (if defined)

pane_output()[source]

Return the contents of the pane.

pane_ps()[source]

Return the ps output for processes running in our pane.

restart(timeout=10)[source]

Relaunch the daemon by sending an arrow up and enter.

start(timeout=10)[source]

Start the daemon.

status()[source]

Return the putative status of the daemon.

Return:
‘running’ if more than one process appear to be running in the daemon’s pane’s tty ‘ready’ if only one process is running in the daemon’s pane’s tty
stop()[source]

Send Ctrl-Cs to the pane the daemon is running on until it stops.

wait_for_output(expected_output, action=None, timeout=10)[source]

Wait (until timeout) for expected output to appear on the pane before returning. If action is specified, it is called every second until the expected output appears.

wait_for_state(state, action=None, timeout=10)[source]

Wait (until timeout) for status to change to the specified state before returning. If action is specified, it is called every second while status is not at state.

daemux.reattach(session, window, pane)[source]

Returns the Daemon Object tied to the specified tmux hierarchy.

daemux.start(cmd, **kwargs)[source]

Start a new daemon and return it.

The daemon is created with the arguments given to start. See Daemon.__init__() for details.

One can give an explicit tmux session/window/pane hierarchy:

>>> import daemux
>>> d = daemux.start(cmd='yes', session='yes', window='yes', pane=-1)
>>> d.stop()