1

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.

1
  • 2
    Try to read the manual. Dec 15, 2016 at 17:16

2 Answers 2

7

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.

2
  • even-vertical is only mentioned once. did you mean to put that on line 3 or 5 perhaps?
    – Tomachi
    May 20, 2020 at 3:01
  • @Tomachi did you try running it? You only need to set the layout after you’re done splitting windows.
    – dpw
    May 21, 2020 at 4:38
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .