更改 bash 脚本的 tmux 键绑定

时间:2021-01-07 14:49:56

标签: tmux

我们确实有一个 bash 脚本,我们可以在集群访问节点上运行该脚本,该脚本在所有集群子节点上执行 htop,以便我们一次监控整个集群。

现在我的问题是,是否有一种方法可以为该脚本绑定 Ctrl-Cq,而无需使用 tmux 前缀。这个想法是,这将使脚本的行为与常规 htop 命令(使用 q 键绑定退出)非常相似,并且其他用户在想要退出时不必深入了解如何使用 tmux 的细节窗户。

我知道,有一种方法可以使用 .tmux.conf 文件更改行为。但是,我们不想全局设置这些键绑定,而只是针对单个脚本。

命令 bash 脚本如下所示:

tmux new -s logs_htop -d ssh-run htop 1
tmux select-pane -T 'cn01'

tmux splitw -v -p 50 -t logs_htop:0.0 ssh-run htop 5
tmux select-pane -T 'cn05'

tmux splitw -h -p 75 -t logs_htop:0.0 ssh-run htop 2
tmux select-pane -T 'cn02'

tmux splitw -h -p 66 -t logs_htop:0.1 ssh-run htop 3
tmux select-pane -T 'cn03'

tmux splitw -h -p 50 -t logs_htop:0.2 ssh-run htop 4
tmux select-pane -T 'cn04'

tmux splitw -h -p 75 -t logs_htop:0.4 ssh-run htop 6
tmux select-pane -T 'cn06'

tmux splitw -h -p 66 -t logs_htop:0.5 ssh-run htop 7
tmux select-pane -T 'cn07'

tmux splitw -h -p 50 -t logs_htop:0.6 "watch squeue -al"
tmux select-pane -T 'squeue'

tmux attach -t logs_htop

1 个答案:

答案 0 :(得分:1)

是的,这可以通过使用自定义 tmux 配置文件连接到不同的 tmux 套接字来实现。

首先,在单独的 conf 文件中创建您的自定义 tmux 键绑定:

mkdir -p /etc/tmux
echo "bind-key -n C-c kill-session" > /etc/tmux/tmux-logs-htop.conf
# do not recommend q as htop is context-sensitive and q does not *always* mean quit
echo "bind-key -n q kill-session" >> /etc/tmux/tmux-logs-htop.conf

接下来,创建您的自定义 tmux 服务器:

tmux -L logs_htop -f /etc/tmux/tmux-logs-htop.conf

最后,编辑您的脚本,为每个命令添加命令将要执行的特定套接字/tmux 服务器的前缀:

tmux -L logs_htop new -s logs_htop -d ssh-run htop 1 
...

来自man tmux

-L socket-name
 tmux stores the server socket in a directory under TMUX_TMPDIR or /tmp if it is unset.
 The default socket is named default.  This option allows a different socket name to be
 specified, allowing several independent tmux servers to be run.  Unlike -S a full path is
 not necessary: the sockets are all created in the same directory.
相关问题