【问题标题】:How do i switch the active tmux session inside a shell script如何在 shell 脚本中切换活动的 tmux 会话
【发布时间】:2020-05-13 12:40:30
【问题描述】:

我正在尝试自动关闭我的 opensimulator 服务器。

在每台服务器上,我都有几个 Tmux 会话。每个会话内部都有几个窗口。在下面的示例中,只有两个会话可以使事情变得简单。

sara@opensim:~$ tmux ls
Robust: 5 windows (created Tue May 12 22:08:28 2020)
Simulators01: 6 windows (created Tue May 12 23:30:38 2020)
sara@opensim:~$

在完整版中,将有 10 多个模拟器会话。

我想要做的是使用一个 shell 脚本来选择一个特定的会话。每个会话都会有一个类似于以下内容的关闭 shell 脚本:

#!/usr/bin/env bash
SESSION="InstancesTesting"
echo "checking for session - $SESSION"
SESSIONEXISTS=$(tmux list-sessions | grep $SESSION)
if [ "$SESSIONEXISTS" != "" ]
then
        echo "session found"
        tmux attach-session -d -t $SESSION
        tmux select-window -t '0821'
        tmux send-keys -t '0821' 'quit' C-m
        tmux select-window -t '0900'
        tmux send-keys -t '0900' 'quit' C-m
        tmux select-window -t '0901'
        tmux send-keys -t '0901' 'quit' C-m
        tmux select-window -t '0910'
        tmux send-keys -t '0910' 'quit' C-m
        tmux select-window -t '0911'
        tmux send-keys -t '0911' 'quit' C-m
        tmux select-window -t '0920'
        tmux send-keys -t '0920' 'quit' C-m
        echo "finished shut down call for $SESSION"
else
   echo "session not found skipping"
fi

问题线是

tmux attach-session -d -t $SESSION

当它从 shell 脚本运行时,之后的一切都会停止,直到会话分离。但是,如果不附加会话,则只能访问最后附加会话的窗口。

我不能简单地在退出命令结束时终止会话,因为在每个窗口内运行的模拟可能需要长达 10 分钟才能关闭。我也不想在每次关机之间等待 10 分钟。我想将它们全部设置好,然后等待进程关闭,然后再重新启动。

我需要的是: 1. 附加会话并允许脚本继续运行,而无需按 ctrl+b D 分离。 或者 2. 更改正在访问的会话,而不像上面的示例那样实际附加它。

我也试过

tmux switch-client SessionName
tmux switch-client -t SessionName
tmux switch-client -n

所有这些都返回相同的结果

no current client

我也试过

tmux send-keys -t 'WindowName' 'tmux choose-session' C-m
tmux send-keys -t 'WindowName' '0' C-m

不幸的是,此选项还表明没有客户端。

我确定这一定是可能的,我在兜圈子,请帮助

【问题讨论】:

    标签: tmux


    【解决方案1】:

    没有“选定会话”的概念,当您没有指定会话时,客户端有一个附加的会话但在 tmux 之外,每次都单独选择要使用的会话。见这里:https://github.com/tmux/tmux/wiki/Advanced-Use#the-default-target

    但你不应该需要它。您已经在使用-t 来指定窗口,也可以使用它来指定会话:

     tmux send-keys -t "$SESSION:8021" 'quit' C-m
    

    您也不需要select-window,除非您稍后计划附加,然后在最后添加一个select-window。有关目标的描述,请参阅https://github.com/tmux/tmux/wiki/Advanced-Use#command-targets

    您可能还会发现has-session 命令有用,而不是使用grep,或-F 标志list-sessions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-25
      • 2019-08-26
      • 1970-01-01
      • 2013-02-05
      相关资源
      最近更新 更多