-2

This seems create a bunch of rows with a single column. I am trying to create 2 columns, and then all the rows needed.

#!/usr/bin/env bash

( unset TMUX

export session_uuid="$(uuidgen)"

remove_tmux_session() { tmux kill-session -t "$session_uuid" }

export -f remove_tmux_session

trap remove_tmux_session TERM INT SIGINT SIGTERM

echo "TMUX session id: $session_uuid" tmux new -d -s "$session_uuid" tmux set-option -g update-environment "remove_tmux_session session_uuid"

Run kubectl get pods and skip the header line

kubectl get pods | tail -n +2 | while read -r line; do # Extract the first column (pod name) pod_name=$(echo "$line" | awk '{print $1}')

  # Run kubectl logs for each pod in a new tmux pane
  tmux split-window -v -t "$session_uuid" "kubectl logs '$pod_name' -f"
  tmux select-layout even-vertical

  # Create a new horizontal split for the second column
  tmux split-window -h -t "$session_uuid" "echo 'Second Column'"

  # Move to the next vertical pane in the new column
  tmux select-pane -t :.+

  # Create a new vertical split for the next row
  tmux split-window -v -t "$session_uuid" "echo 'Next Row'"

  # Move to the next horizontal pane in the new row
  tmux select-pane -t :.+

  # Repeat the process for additional rows/columns as needed

done

Attach to the tmux session

tmux attach-session -d -t "$session_uuid" )

anyone know how to create 2 columns and then put each process inside a different row in the 2 column grid?

I am on Macos, Tmux version 3.3a

muru
  • 72,889
  • 1
    Your description is confusing to me, but given your cmds tmux split-window ..., tmux select-layout even-vertical .... and tmux select-pane ... etc, it sounds self-describing. Do these cmds work from the basic cmd-line (without the kubectl get pods ... | while ... wrapper? One of the few times a graphic image could help with question? Or at least an ascii-art drawing? Is there another term? I think of columns being inside of terminal's border. Good luck. – shellter Feb 06 '24 at 01:05
  • 1
    tmux split-window -h -t "$session_uuid" "echo 'Second Column'" – A pane that only executes echo will exit immediately and be destroyed, unless remain-on-exit tmux option is on for it. Please confirm you set this option to on and the behavior I described is not a part of the problem. – Kamil Maciorowski Feb 06 '24 at 18:24
  • ahh that makes sense - that's really strange behavior – Alexander Mills Feb 06 '24 at 20:17
  • "that's really strange behavior" – It really depends on what you expect or want. See "Terminating the tmux session" in this answer of mine. It seems the asker there wanted exactly the default behavior. You want something else, you need to change the option(s). Neither behavior is strange, there are use cases for each. – Kamil Maciorowski Feb 07 '24 at 14:06
  • it's the same concept as starting an ssh session and trying to run a command, I would expect it to say open like opening a terminal app. If you want to exit immediately, then put "exit x", or use a switch to exist – Alexander Mills Feb 07 '24 at 16:53

1 Answers1

2

It's not too clear what layout you are looking for, but here is a demo bash script that may provide you with some ideas. One problem is that select-layout even-vertical will remove any column structure you have developed, and place all the panes vertically. So the approach here is to first create the vertical structure of rows, equalise their spacing, then split each pane horizontally. This image shows the result. The text shown in each pane is just a hint as to the real command you would run there, and the aa bb cc are just a simplified pod_name from your code. Each pane is titled with the pane_index which changes all the time, and the %pane_id which does not.

xterm tmux layout

Here is the bash script, which shouldn't be too exotic for MacOS, but you'll have to try for yourself.

#!/bin/bash
session_uuid=mysession
DISPLAY=:0 xterm -title ttmux -geometry 60x30-1+1 \
  -e "tmux new-session -s $session_uuid  'sleep 20'" &
sleep 2
tmux set -g pane-border-status bottom
tmux set -g pane-border-format "#{pane_index} #{pane_id}"

declare -a names for pod_name in aa bb cc do names+=($pod_name) done

declare -A pane_ids for pod_name in ${names[@]} do tmux split-window -v -t "$session_uuid" "echo kubectl logs '$pod_name';sleep 999" pane_ids[$pod_name]=$(tmux display -p '#{pane_id}') done

tmux kill-pane -t "$session_uuid:0.0" tmux select-layout even-vertical # removes columns!

for pod_name in ${names[@]} do pane_id=${pane_ids[$pod_name]} tmux split-window -h -t "$pane_id" "echo 'Second Column' $pod_name;sleep 999" tmux display-panes done

The script starts by creating a new terminal to run tmux with a dummy command, just so it is easy to see the progress. In the first for loop, the bash array names is used to collect the pod_name, as in the loop in your script. The second for loop creates a new vertical pane, and "runs" the "kubectl" command. It retrieves the unique pane_id and saves it in the bash associative array pane_ids, using the pod_name as key.

We then kill the dummy pane, and spread the panes out evenly with select-layout even-vertical. The final for loop splits each pane horizontally, using the pod_name and unique pane_id, and "runs" some other command.

meuh
  • 51,383