tmux attach to existing sessions or create new onesDoes tmux have all the features that screen has? And those screen is missing?freeze and thaw tmux and/or terminal sessions?Linux: Connect to another a terminal sessionWhat is causing the famous “broken pipe” when I use tmux inside an SSH session?Tmux new-session returns: “can't create socket”Linux “tmux” Permission deninedSend command to new tmux pane?Easing terminal sizing on OpenVMSByobu creating new session on every connectionUsing screen/tmux to ssh into remote computers and issue a sudo elevated script for patching
Why is a blank required between "[[" and "-e xxx" in ksh?
How should I behave to assure my friends that I am not after their money?
Do I have to roll to maintain concentration if a target other than me who is affected by my concentration spell takes damage?
How do I spend money in Sweden and Denmark?
Quacks of Quedlingburg Crow Skull Set 2 Keep Drawing
A way to connect Microsoft Green-Eyed mouse to modern computer?
What is a macro? Difference between macro and function?
Averting Real Women Don’t Wear Dresses
Procedurally generate regions on island
What shortcut does ⌦ symbol in Camunda macOS app indicate and how to invoke it?
How to start learning the piano again
Should the Torah be covered or uncovered during the Aliyah blessings?
How hard is it to sell a home which is currently mortgaged?
The difference between Rad1 and Rfd1
How to fix a dry solder pin in BGA package?
Does “comme on était à New York” mean “since” or “as though”?
Why is Madam Hooch not a professor?
Can a US President have someone sent to prison?
Can you sign using a digital signature itself?
Does the UK have a written constitution?
Does a centaur PC also count as being mounted?
Symbol for "not absolutely continuous" in Latex
In F1 classification, what is ON?
Should I hide continue button until tasks are completed?
tmux attach to existing sessions or create new ones
Does tmux have all the features that screen has? And those screen is missing?freeze and thaw tmux and/or terminal sessions?Linux: Connect to another a terminal sessionWhat is causing the famous “broken pipe” when I use tmux inside an SSH session?Tmux new-session returns: “can't create socket”Linux “tmux” Permission deninedSend command to new tmux pane?Easing terminal sizing on OpenVMSByobu creating new session on every connectionUsing screen/tmux to ssh into remote computers and issue a sudo elevated script for patching
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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.
tmux
add a comment |
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.
tmux
add a comment |
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.
tmux
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.
tmux
tmux
asked Sep 16 '13 at 12:03
sorinsorin
3,56220 gold badges55 silver badges85 bronze badges
3,56220 gold badges55 silver badges85 bronze badges
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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
2
This one is the most simple and it does the job, using only one command instead of the 2+ with thetmux attach || tmux new
combinations. Cheers!
– SidOfc
Dec 6 '17 at 8:14
add a comment |
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:
- calls
tmux ls
and parses the output, looking for something that is not attached - attach to the first non-attached session, -OR-
- 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
add a comment |
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
add a comment |
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
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "2"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f539223%2ftmux-attach-to-existing-sessions-or-create-new-ones%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
2
This one is the most simple and it does the job, using only one command instead of the 2+ with thetmux attach || tmux new
combinations. Cheers!
– SidOfc
Dec 6 '17 at 8:14
add a comment |
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
2
This one is the most simple and it does the job, using only one command instead of the 2+ with thetmux attach || tmux new
combinations. Cheers!
– SidOfc
Dec 6 '17 at 8:14
add a comment |
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
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
answered Jan 2 '16 at 21:00
EdoEdo
1312 bronze badges
1312 bronze badges
2
This one is the most simple and it does the job, using only one command instead of the 2+ with thetmux attach || tmux new
combinations. Cheers!
– SidOfc
Dec 6 '17 at 8:14
add a comment |
2
This one is the most simple and it does the job, using only one command instead of the 2+ with thetmux attach || tmux new
combinations. Cheers!
– SidOfc
Dec 6 '17 at 8:14
2
2
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
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
add a comment |
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:
- calls
tmux ls
and parses the output, looking for something that is not attached - attach to the first non-attached session, -OR-
- 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
add a comment |
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:
- calls
tmux ls
and parses the output, looking for something that is not attached - attach to the first non-attached session, -OR-
- 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
add a comment |
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:
- calls
tmux ls
and parses the output, looking for something that is not attached - attach to the first non-attached session, -OR-
- 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.
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:
- calls
tmux ls
and parses the output, looking for something that is not attached - attach to the first non-attached session, -OR-
- 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.
answered Sep 17 '13 at 1:47
Joe CasadonteJoe Casadonte
2563 silver badges15 bronze badges
2563 silver badges15 bronze badges
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
add a comment |
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
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
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
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
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Apr 20 '17 at 23:09
user1624870user1624870
111 bronze badge
111 bronze badge
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jun 10 at 11:22
thilothilo
111 bronze badge
111 bronze badge
add a comment |
add a comment |
Thanks for contributing an answer to Server Fault!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f539223%2ftmux-attach-to-existing-sessions-or-create-new-ones%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown