How does tmux spawn multiple sessions?

2

0

I noticed something peculiar while working with multiple tmux sessions. I'm not certain on all the terminology so please correct me if I get something wrong.

Creating tmux sessions beyond the first does not inherit the environment it was created from, they inherit the environment that spawned the first session.

export a=false; export b=false

bash
export a=true
tmux new-session -d -s first

bash
export b=true
tmux new-session -d -s second

then executing echo $a $b from either session gives the output true false. I was expecting true true for the second session.

I don't know how tmux "remembers" the environment of the first session. I can perform another experiment:

If I update the environment in the first session, detach, and spawn a second session, the second session does not inherit the update.

And another experiment:

If I exit the subshell after creating the first session (thus "forgetting" the environment variable a), and spawn a second session, the second session still remembers the environment.

I'd like to better know why this is happening and some of what tmux is doing under-the-hood, so any advice in that direction is appreciated.

roro

Posted 2019-04-10T00:45:03.460

Reputation: 123

Can you spawn Bash as bash -l? I don’t believe environment variables are passed unless the -l parameter is there. – JakeGould – 2019-04-10T00:57:53.377

This had no impact for me. On my system bash starts an interactive session by default and will source .profile if passed with -l – roro – 2019-04-10T01:20:56.323

Answers

2

There is a section named ENVIRONMENT in man 1 tmux:

When the server is started, tmux copies the environment into the global environment; in addition, each session has a session environment. When a window is created, the session and global environments are merged. If a variable exists in both, the value from the session environment is used. The result is the initial environment passed to the new process.

The update-environment session option may be used to update the session environment from the client when a new session is created or an old reattached. […]

Commands to alter and view the environment are:

set-environment […]

show-environment […]

And where the manual explains set-option, it reads:

update-environment variables

Set a space-separated string containing a list of environment variables to be copied into the session environment when a new session is created or an existing session is attached.

[…] The default is "DISPLAY SSH_ASKPASS SSH_AUTH_SOCK SSH_AGENT_PID SSH_CONNECTION WINDOWID XAUTHORITY".

So your a and b are copied to tmux only when the server starts. In your case it's when you create the first session. These variables are not on the list stored by update-environment option, so later they are not updated.

If you did

tmux set-option -t second update-environment "a b"

and then attached to this session, your current variables would be absorbed by tmux. This doesn't mean echo $a $b would show them in the shell that had already been started (by tmux new-session -d -s first). But a new shell (or any other process) in a new pane would inherit them from tmux.

It's possible to set the option globally. See the OPTIONS section. But keep these things in mind:

  • the option is designed to work when a new session is created or an old reattached (although you can change the behavior with -E option of attach-session; few other commands also support -E);
  • the updated value won't affect existing processes (panes).

To summarize: in general tmux sessions and processes within don't simply inherit the environment from the client like a simple child process would do.

Kamil Maciorowski

Posted 2019-04-10T00:45:03.460

Reputation: 38 429