So, let's say for example, I need to run a tmux socket between two users that runs

echo foo

To make this run on a normal tmux, it'd be best to do tmux new -d -s Foo "echo foo"

However, when running tmux using -S, I can't seem to find a way to automatically run a command as the session starts. Let's say our path is /home/user/tmux

I'd run tmux -S /home/user/tmux, and then on another account tmux -S /home/user/tmux attach. However, this doesn't leave an opportunity to specify a starting command. Any way I could do this?



So one approach is to spawn a new window and then attach:

tmux -S /tmp/socket new-window -n vi /usr/bin/vi
tmux -S /tmp/socket attach

So the client can create a new window and then connect to it.

However, I suspect that a tmux session might not be doing what you want it to do.

The reason that attach has no command is because a session represents a view what is currently seen on the screen, which is shared between all connected clients. So attaching with a command is a little weird. All clients will view the same thing, and the new-window call will change the view for all attached clients.

If you want to share a collection of windows (e.g views of running programs), but have each client have a distinct view then you need to create sessions. You can use the -t argument to new-session to make a session that shares its windows with another session. Apparently you can't use -t together with a command for the new session, which is actually a valid use case, so you end up using the -d flag and then spawning the command. To make things even more irritating it would appear that new-window lacks a session argument, so you instead rely on the order that things are called!

tmux -S /tmp/socket new-session -s base  # run once

# For each client
tmux -S /tmp/socket new-session -t client-view -d 
tmux -S /tmp/socket new-window /usr/bin/vi
tmux -S /tmp/socket attach -t client-view

This is all a bit of kludge put I have managed to use this sort of approach effectively for a number of years. (Each of my terminal windows is a distinct view into a shared tmux session).

You might be interested in this script https://github.com/rainerborene/tmx/blob/master/tmx that wraps all of this up into a single shell script.

  • All the tmuxlib python library might be of tangential interest for you if you are trying to do complicated things with tmux. – Att Righ Feb 11 '17 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.