12

I would like to know if there is a way to get tmux to behave like screen -D -R so I could say, have the command as a default command in Putty.

These screen switches would force detach of an existing screen session for my user (even if it was still active and logged-in somewhere else) and reattach it to the current session. Also, in the case that no screen session existed, it would create a new one.

I like tmux and can see clear benefits over screen, but the existence of this feature would really seal the deal.

tmux attach doesn't seem to create a new session if there isn't one.

The man page for tmux says:

If no server is started, attach-session (attach) will attempt to start it; this will fail unless sessions are created in the configuration file.

What does the section in bold mean? (I can't find an example of creating a session in the conf file).

  • From the man page: "The configuration file is a set of tmux commands which are executed in sequence when the server is first started." -- you could actually have attach or new-session as a line in your .tmux.conf. – sr_ Mar 5 '12 at 13:35
  • @sr_ Well, the when the server is first started new-session would have just happened anyway... – Chris Down Mar 5 '12 at 13:36
  • Just found this (closed) question on SO. Same question with similar answers. stackoverflow.com/q/3432536/168034 – phunehehe May 29 '13 at 12:34
14

Yes:

$ tmux attach -d || tmux new

-d is necessary to behave like screen -D, ie, detach everybody else.

Connect by ssh, then attach or create could be something like:

$ cat bin/stmux
#!/bin/sh
exec ssh -t $@ 'tmux attach -d || tmux new'

$ stmux my.remote.box
  • 1
    Yes - this seems to be the most effective way to achieve the what I requested. Likewise, I disapprove of new-session in the config file in case you want to edit it and reload during a tmux session. – Geeb Jan 16 '14 at 10:07
4

To make tmux attach create a new session when there isn't one, use the option new-session in the tmux config file. Create the file ~/.tmux.conf if it doesn't exist, and add

new-session

to it. Also, I alias tmux to tmux attach :)

  • 1
    I prefer not to have new-session in ~/.tmux.conf because if I reload the configuration using command source-file ~/.tmux.conf (see this) a new session will be launched. Yes, I can kill it and then attach to the previous one but that is a little annoying. – mmoya May 14 '12 at 11:04
3

You can emulate this with a shell function, this should work for any POSIX-compliant shell:

tmux() {
    if [ "$#" -ge 1 ] && [ "$1" = -z ]; then
        shift
        command tmux detach 2>/dev/null
        command tmux attach "$@" || command tmux new-session "$@"
    else
        command tmux "$@"
    fi
}

Now if you launch it as tmux -z, it should perform the actions you're looking for.

2

this one works a bit better for me:

tmux new-session -AD -s <session-name>
1

I find this works best for me on .bashrc

if [[ -z $TMUX ]]; then
  tmux attach-session || tmux new-session
fi
  • How does that explain the bold section in the OP? – Anthon Oct 14 '13 at 5:53
0

If you're using this inside a .shrc file or similar with exec I'd recommend

if tmux ls
  exec tmux attach
else
  exec tmux
fi

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.