I am looking for a ssh / tmux solution that would act like this:

  • if there is no session, create one
  • if there is a session and nobody is connected to it, create another one

Mainly I want to be able to create new sessions to the same server, obviously if there is more than one session that has nobody connected to it, it should pick the first one.

This should enable me to put this as default command for ssh connections.

My current solution ssh -t 'tmux a || tmux || /bin/bash' doesn't work as expected because when you try to connect again it will connect to the existing session, and in this case I want a new one.


I'm not sure since what versione but now you can use

tmux new -A -s <session-name>

The -A flag makes new-session behave like attach-session if session-name already exists

  • 3
    This one is the most simple and it does the job, using only one command instead of the 2+ with the tmux attach || tmux new combinations. Cheers! – SidOfc Dec 6 '17 at 8:14

That's kind of an odd use-case, but what you'd need to do is write a wrapper around tmux (call it mytmux or something) that:

  1. calls tmux ls and parses the output, looking for something that is not attached
  2. attach to the first non-attached session, -OR-
  3. create a session if no free sessions are found and attach to it

The command tmux ls should return something like this if there are any sessions:

<~> $ tmux ls
0: 1 windows (created Mon Sep 16 21:42:16 2013) [120x34] (attached)

where the initial field ('0') is the session name and the last field denotes whether anyone is attached to it. So if no one was attached it would look like this:

<~> $ tmux ls
0: 1 windows (created Mon Sep 16 21:42:16 2013) [120x34]

and if some were attached and some not, you'd get:

<~> $ tmux ls
0: 1 windows (created Mon Sep 16 21:42:16 2013) [120x34] (attached)
1: 1 windows (created Mon Sep 16 21:43:30 2013) [120x34]

If you find no sessions at all or no free sessions, run tmux new to create one. If you find a free session, run tmux attach -t 1 where '1' is the name of the free session.

  • Thank you, I don't know why this would be an odd use-case, but I will try to implement it myself. The real trick would be to implement this in a single command so I will be able to use this without having to deploy anything on the target servers. – sorin Sep 17 '13 at 8:52
  • I guess I use a tmux session for a specific purpose, and if ssh into a machine I would want to attach to a specific session. If you leave two or more sessions unattached, you would get a random (albeit deterministic) session using your method. I would want to connect to a specific session. So perhaps I should say that this is an odd use-case for me. In any event, You can keep the script local and do all of the logic locally by calling ssh multiple times, e.g. capture the output from "ssh user@host tmux ls", parse it and decide what to do with it, all on the local machine. – Joe Casadonte Sep 17 '13 at 23:45
  • If you're really desperate for a fully-functional script I can probably throw together a perl script pretty easily. – Joe Casadonte Sep 17 '13 at 23:47

I also needed the 're-use any detached session or create one' feature. Here's my one-liner for this (will fail miserably if you use ":" in session name):

tmux attach -t $(tmux ls | grep -v attached | head -1 | cut -f1 -d:) || tmux

The OP's post is a bit confusing but from the original solution "tmux a || tmux || bash" I deduct: attach to existing or create new one =>

tmux ls | grep -v attached && tmux attach || tmux

will do.

I prefer: "if an un attached tmux session exists, connect to it, else shell" in .profile:

tmux ls | grep -v attached && tmux attach

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.