Servers#

  • Identified by socket path and socket name

  • May have >1 servers running of tmux at the same time.

  • Contain Sessions (which contain Windows, which contain Panes)

tmux initializes a server on automatically on first running (e.g. executing tmux)

class libtmux.Server(socket_name=None, socket_path=None, config_file=None, colors=None, **kwargs)[source]#

Bases: libtmux.common.TmuxRelationalObject, libtmux.common.EnvironmentMixin

The tmux(1) Server [server_manual].

When instantiated stores information on live, running tmux server.

Parameters:
  • socket_name (str, optional) –

  • socket_path (str, optional) –

  • config_file (str, optional) –

  • colors (str, optional) –

References

[server_manual]
CLIENTS AND SESSIONS. openbsd manpage for TMUX(1)

“The tmux server manages clients, sessions, windows and panes. Clients are attached to sessions to interact with them, either when they are created with the new-session command, or later with the attach-session command. Each session has one or more windows linked into it. Windows may be linked to multiple sessions and are made up of one or more panes, each of which contains a pseudo terminal.”

https://man.openbsd.org/tmux.1#CLIENTS_AND_SESSIONS. Accessed April 1st, 2018.

_add_option#
_list_panes(self)[source]#

Return list of panes in dict form.

Retrieved from $ tmux(1) list-panes stdout.

The list is derived from stdout in util.tmux_cmd which wraps subprocess.Popen.

Return type:

list

_list_sessions(self)[source]#

Return list of sessions in dict form.

Retrieved from $ tmux(1) list-sessions stdout.

The list is derived from stdout in common.tmux_cmd which wraps subprocess.Popen.

Return type:

list of dict

_list_windows(self)[source]#

Return list of windows in dict form.

Retrieved from $ tmux(1) list-windows stdout.

The list is derived from stdout in common.tmux_cmd which wraps subprocess.Popen.

Return type:

list of dict

property _sessions(self)#

Property / alias to return _list_sessions().

_update_panes(self)[source]#

Update internal pane data and return self for chainability.

Return type:

Server

_update_windows(self)[source]#

Update internal window data and return self for chainability.

Return type:

Server

attach_session(self, target_session=None)[source]#

$ tmux attach-session aka alias: $ tmux attach.

Parameters:

target_session (str) – name of the session. fnmatch(3) works.

Raises:

exc.BadSessionName

property attached_sessions(self)#

Return active Session objects.

Return type:

list of Session

child_id_attribute = session_id#
children#
cmd(self, *args, **kwargs)[source]#

Execute tmux command and return output.

Return type:

common.tmux_cmd

Notes

Changed in version 0.8: Renamed from .tmux to .cmd.

colors#
config_file#
find_where(self, attrs)[source]#

Return object on first match.

Changed in version 0.4: Renamed from .findWhere to .find_where.

formatter_prefix = server_#
get_by_id(self, id)[source]#

Return object based on child_id_attribute.

Parameters:

val (str) –

Return type:

object

Notes

Based on `.get()`_ from `backbone.js`_.

has_session(self, target_session: str, exact: bool = True)[source]#

Return True if session exists. $ tmux has-session.

Parameters:
  • target_session (str) – session name

  • exact (bool) – match the session name exactly. tmux uses fnmatch by default. Internally prepends = to the session in $ tmux has-session. tmux 2.1 and up only.

Raises:

exc.BadSessionName

Return type:

bool

kill_server(self)[source]#

$ tmux kill-server.

kill_session(self, target_session=None)[source]#

Kill the tmux session with $ tmux kill-session, return self.

Parameters:

target_session (str, optional) – target_session: str. note this accepts fnmatch(3). ‘asdf’ will kill ‘asdfasd’.

Return type:

Server

Raises:

exc.BadSessionName

list_sessions(self)[source]#

Return list of Session from the tmux(1) session.

Return type:

list of Session

new_session(self, session_name=None, kill_session=False, attach=False, start_directory=None, window_name=None, window_command=None, *args, **kwargs)[source]#

Return Session from $ tmux new-session.

Uses -P flag to print session info, -F for return formatting returns new Session object.

$ tmux new-session -d will create the session in the background $ tmux new-session -Ad will move to the session name if it already exists. todo: make an option to handle this.

Parameters:
  • session_name (str, optional) –

    $ tmux new-session -s <session_name>
    

  • attach (bool, optional) –

    create session in the foreground. attach=False is equivalent to:

    $ tmux new-session -d
    

  • kill_session (bool, optional) – Kill current session if $ tmux has-session. Useful for testing workspaces.

  • start_directory (str, optional) – specifies the working directory in which the new session is created.

  • window_name (str, optional) –

    $ tmux new-session -n <window_name>
    

  • window_command (str) – execute a command on starting the session. The window will close when the command exits. NOTE: When this command exits the window will close. This feature is useful for long-running processes where the closing of the window upon completion is desired.

Return type:

Session

Raises:

exc.BadSessionName

remove_environment(self, name)[source]#

Remove environment variable $ tmux set-environment -r <name>.

Parameters:

name (str) – the environment variable name. such as ‘PATH’.

property sessions(self)#

Property / alias to return list_sessions().

set_environment(self, name, value)[source]#

Set environment $ tmux set-environment <name> <value>.

Parameters:
  • name (str) – the environment variable name. such as ‘PATH’.

  • option (str) – environment value.

show_environment(self, name=None)[source]#

Show environment $ tmux show-environment -t [session] <name>.

Return dict of environment variables for the session or the value of a specific variable if the name is specified.

Parameters:

name (str) – the environment variable name. such as ‘PATH’.

Returns:

environmental variables in dict, if no name, or str if name entered.

Return type:

str or dict

socket_name#
socket_path#
switch_client(self, target_session)[source]#

$ tmux switch-client.

Parameters:

target_session (str) – name of the session. fnmatch(3) works.

Raises:

exc.BadSessionName

unset_environment(self, name)[source]#

Unset environment variable $ tmux set-environment -u <name>.

Parameters:

name (str) – the environment variable name. such as ‘PATH’.

where(self, attrs, first=False)[source]#

Return objects matching child objects properties.

Parameters:

attrs (dict) – tmux properties to match values of

Return type:

list