Is it possible to make a bash script that starts tmux and split the screen horizontally and runs watch -n1 tail -n5 file_n
in each ? Basically I'm starting a script multiple times and write its progress into different files that I'd like to monitor. Would be nice if I could run that from one script as opposed to manually open 10 files by myself. I never used tmux btw that's why I'm asking this.
-
2Try to read the manual.– Ipor SircerCommented Dec 15, 2016 at 17:16
2 Answers
Try this. It first establishes a detached tmux session, then opens your windows with tail commands, then sets the layout of the windows, then attaches to the session.
for f in `seq 1 10`; do
if [[ $f -eq 1 ]]; then
tmux new-session -d -s my_session_name "watch -n1 tail -n5 file_${f}"
else
tmux split-window -d -t my_session_name:0 -p20 -v "watch -n1 tail -n5 file_${f}";
fi
done
tmux select-layout -t my_session_name:0 even-vertical
tmux attach-session -t my_session_name
If you want to have multiple instances of this run, you need to change all the occurences of my_session_name
to be something unique for each session.
Also, your title mentions 5 windows but the body of your post mentions 10 files. The code as-is will open 10 files in 10 windows. Change the seq 1 10
part for however many windows/files you actually want.
Thanks to @Kusalananda for the initial script. I added some extra things that I needed (e.g. custom pane title based on ssh ip):
#!/bin/bash
set -ex
key_path="your-key.pem"
ssh_user="user"
ssh_list=(1.2.2.2 3.3.3.2 5.5.5.6)
split_list=()
for ssh_entry in "${ssh_list[@]}"; do
ssh-keygen -R $ssh_entry
split_list+=( split-window ssh -i ${key_path} -o StrictHostKeyChecking=no ${ssh_user}@$ssh_entry ';' select-pane -T "-- $ssh_entry " ';')
done
tmux set -g pane-border-status top
tmux set -g pane-border-format "#{pane_index} #{pane_current_command} #{pane_title}"
tmux new-session "Multi-Session-Ssh" ';' \
"${split_list[@]}" \
select-layout tiled ';' \
set-option -w synchronize-panes