如何检测 tmux 是否附加到 bash 中的会话?
How to detect if tmux is attached to a session in bash?
我有一个 .sh
文件,它为 tmux 创建一个新会话并添加一些 windows,只有在没有会话存在时才应使用该文件。例如:
tmux new-session -A -s `ax` -n ui -d
# add windows and other magic here...
我想防止创建同名会话并重新创建 windows,以防意外重新执行 .sh
文件并且会话为 运行.
基本上我需要的是:
If a tmux session ax does not exist with that session name, create that session. If
I am not attached to a tmux session, attach to that session.
我想知道如何检测 tmux 会话是否存在以及 tmux 是否附加到它,在这个例子中 ax
是 运行 并阻止执行 .sh
脚本或者如果会话没有退出我想重新执行 .sh
脚本。
目前我想使用:
tmux ls | grep attached
我想知道你是否知道更好的方法。
有点难以理解你的意思。我将其解释为
if a tmux session does not exist with that session name, create it. If I am not attached to a tmux session, attach to that session name.
如有不妥,请指正。
我的脚本中有类似的功能。我所做的只是
tmuxstart() {
tmux ls | grep "sess" && { tmux a -t sess; return 0; }
#rest of tmux script to create session named "sess"
tmux a -t sess
}
如果名为 "sess" 的会话存在,那么我会执行接下来的 2 个分组命令(附加到它并退出函数)。
请注意,我不是必须检查我是否已经附加到函数。 tmux 会自动执行此操作。如果您在会话中尝试附加到 tmux 会话,它将响应
sessions should be nested with care, unset $TMUX to force
并且不递归附加。 Tmux 足够聪明,可以防止我们搬起石头砸自己的脚。
可以用$TMUX
检测是否已经附加,我的代码是:
if [ ! "$TMUX" ]; then
tmux attach -t main || tmux new -s main
fi
我有一个 .sh
文件,它为 tmux 创建一个新会话并添加一些 windows,只有在没有会话存在时才应使用该文件。例如:
tmux new-session -A -s `ax` -n ui -d
# add windows and other magic here...
我想防止创建同名会话并重新创建 windows,以防意外重新执行 .sh
文件并且会话为 运行.
基本上我需要的是:
If a tmux session ax does not exist with that session name, create that session. If I am not attached to a tmux session, attach to that session.
我想知道如何检测 tmux 会话是否存在以及 tmux 是否附加到它,在这个例子中 ax
是 运行 并阻止执行 .sh
脚本或者如果会话没有退出我想重新执行 .sh
脚本。
目前我想使用:
tmux ls | grep attached
我想知道你是否知道更好的方法。
有点难以理解你的意思。我将其解释为
if a tmux session does not exist with that session name, create it. If I am not attached to a tmux session, attach to that session name.
如有不妥,请指正。
我的脚本中有类似的功能。我所做的只是
tmuxstart() {
tmux ls | grep "sess" && { tmux a -t sess; return 0; }
#rest of tmux script to create session named "sess"
tmux a -t sess
}
如果名为 "sess" 的会话存在,那么我会执行接下来的 2 个分组命令(附加到它并退出函数)。
请注意,我不是必须检查我是否已经附加到函数。 tmux 会自动执行此操作。如果您在会话中尝试附加到 tmux 会话,它将响应
sessions should be nested with care, unset $TMUX to force
并且不递归附加。 Tmux 足够聪明,可以防止我们搬起石头砸自己的脚。
可以用$TMUX
检测是否已经附加,我的代码是:
if [ ! "$TMUX" ]; then
tmux attach -t main || tmux new -s main
fi