自动启动四分割 byobu 终端

自动启动四分割 byobu 终端

我希望通过自动完成以下操作来避免浪费时间进行启动仪式:

  1. 打开一个新终端
  2. 运行 byobu
  3. 水平分割,然后垂直分割两个新窗格(反之亦然)
  4. 为每个窗格运行特定命令

我猜想它将会是这样的:

gnome-terminal --full-screen -- byobu -S MainSession

byobu-tmux select-pane -t 0
byobu-tmux split-window -v
byobu-tmux select-pane -t 1
byobu-tmux split-window -h
byobu-tmux select-pane -t 0
byobu-tmux split-window -h

byobu-tmux select-pane -t 1
byobu-tmux send-keys "COMMAND"
byobu-tmux select-pane -t 2
byobu-tmux send-keys "COMMAND"
byobu-tmux select-pane -t 3
byobu-tmux send-keys "COMMAND"
byobu-tmux select-pane -t 0

第一行本身会打开一个新的全屏终端并将新的 byobu 会话命令传递给它。但是,我不知道如何将脚本的其余部分连接在一起。如果我在 byobu 之前放置一个开头的引号,用 ; 分隔所有命令,并在脚本末尾放置一个结尾的引号,那么终端就会在没有 byobu 的情况下打开,并出现错误:“无法执行子进程(没有这样的文件或目录)”。

此外,如何在特定显示器上打开终端?根据 gnome-control-center,我希望在 3 号显示器上打开它。

答案1

我花了一段时间才弄明白,所以如果有人需要一个启动脚本来打开多个 byobu 会话,请根据需要使用和修改:

#Create new session. I named this LeftMonitor for obvious reasons
byobu new-session -d -s LeftMonitor

#Select default pane. Probably an unnecessary line of code
byobu select-pane -t 0

#Split pane 0 into two vertically stacked panes
byobu split-window -v

#Select the newly created pane 1. Again, probably unnecessary as the new pane gets selected after a split
byobu select-pane -t 1

#Split pane 1 horizontally to create two side-by-side panes
byobu split-window -h

#Repeat the selection and splitting process with the top half
byobu select-pane -t 0
byobu split-window -h
#At this point, four equally sized panes have been created.

#Select pane to interact with
byobu select-pane -t 1

#Pass a command to the selected pane. I'm using top as the example here.
#Note that you need to type Enter for byobu to simulate pressing the enter key.
byobu send-keys "top" Enter

#Create a new session. Session name is again chosen for obvious reasons
byobu new-session -d -s RightMonitor

#Repeat the same splitting and command issuing processes from the first session.
byobu select-pane -t 0
byobu split-window -h
byobu select-pane -t 1
byobu send-keys "top" Enter
byobu select-pane -t 0
byobu send-keys "top" Enter

#Finally, to be able to actually see anything, you need to launch a terminal for each session
gnome-terminal --full-screen -- byobu attach -t LeftMonitor
gnome-terminal --full-screen -- byobu attach -t RightMonitor

使用您喜欢的文本编辑器保存它,在文件上运行 sudo chmod +x,然后将其添加到您使用的任何启动列表中。

答案2

我做了一些修改。希望有用。

我创建 -sudo nano /主页/替换为你的家目录/.byobu/setup_on_boot.sh:

#!/bin/bash

# Check if the crypto-work session already exists
if byobu list-sessions | grep -q "crypto-work"; then
    echo "crypto-work session already exists. Skipping setup."
    exit 0
fi

# Create new session
byobu new-session -d -s crypto-work

# Rename first windows
byobu rename-window -t 0 Terminal

###################################
# Binance Spot and Futures        #
###################################
byobu new-window -n Binance
# Split window horisontally
byobu split-window -h
# Select pane 0
byobu select-pane -t 0
# Starting Python script
byobu send-keys "cd /home/*replace_by_your_home_dir*/Binance-Spot" Enter
byobu send-keys "source /home/*replace_by_your_home_dir*/Binance-Spot/venv/bin/activate" Enter
byobu send-keys "python binance_spot.py" Enter
# Select pane 1
byobu select-pane -t 1
# Starting Python script
byobu send-keys "cd /home/*replace_by_your_home_dir*/Binance-Fut" Enter
byobu send-keys "source /home/*replace_by_your_home_dir*/Binance-Fut/venv/bin/activate" Enter
byobu send-keys "python binance_fut.py" Enter

byobu detach

然后编辑/家/替换为你的家目录/。轮廓-sudo nano /主页/替换为你的家目录/。轮廓并添加几行到底文件:

# Check if the setup script has already been executed
if [ ! -f "$HOME/.byobu/setup_executed" ]; then
    # Run the setup script
    /bin/bash /home/*replace_by_your_home_dir*/.byobu/setup_on_boot.sh
    # Mark the setup script as executed
    touch "$HOME/.byobu/setup_executed"
fi
# Launch the Byobu session with the crypto-work session name
_byobu_sourced=1 . /usr/bin/byobu-launch attach -t crypto-work 2>/dev/null || true

因此,当我通过 SSH 连接时,我得到了一个现成的布局,其中有所有需要的窗口、窗格 + python venv 已激活 + py 脚本已在运行。 不要忘记:chmod +x setup_on_boot.sh

相关内容