Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

Change tmux key-binding for bash script

We do have a bash script, which we can run on a cluster access node, that executes htop on all cluster child nodes, in order for us to monitor the whole cluster at once.

Now my question is, whether, there is a way to bind Ctrl-C or q for this script, without having to use the tmux prefix. The idea is, that this would make the script behave very similar to the regular htop command (which uses the q key binding to quit), and other users would not have to dive into the specifics of how to use tmux, when wanting to quit the window.

I am aware, that there is a way to change the behaviour using the .tmux.conf file. However, we do not want to set these keybinding globally, but only for this single script.

The command bash script looks like this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, this is possible by connecting to a different tmux socket with a custom tmux config file.

First, create your custom tmux keybind in a separate conf file:

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

Next, create your custom tmux server:

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

Lastly, edit your script to prefix every command with the particular socket / tmux server the commands are going to:

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

From 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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...