在 bash 脚本中分离带有调试程序的 tmux 会话

Detach tmux session w/ debugging program inside bash script

我有一个我想要 运行 的脚本,我将在其中启动两个程序 运行 在单独的 tmux 会话中。我现在的脚本差不多是:

!/bin/bash
tmux new -s test1 'mono --debug program1.exe'
tmux new -s test2 'python program2.py'

我遇到的问题是这 2 个程序 运行 处于调试模式,因此它们正在主动向 tmux 会话输出信息。启动程序后,我无法控制在 tmux 会话中键入任何内容。但是,我可以使用 Ctrl + b d 方法手动分离会话。不过,我不确定如何在 bash 脚本中执行此操作。

我找到了 tmux detach 命令,但由于我不确定在程序启动和正在输出调试信息后如何输入会话,所以我无法输入该命令。

我还发现了一个 post 说有一个 -d 标志可以用于 tmux,它将启动一个分离的会话,我希望我可以做类似 tmux new -d test1 'mono --debug program1.exe' 但这似乎没有用。它似乎在抱怨 -d 标志中的语法。

要启动两个单独的会话,每个会话 运行 一个程序或命令,而不附加,请尝试在您的 script.sh:

中写入
#!/bin/bash

tmux \
    new \
        -d \
        -s test1 \
        'mono --debug program1.exe' \
    \; \
    new \
        -s test2 \
        -d \
        'python program2.py'

说明

  • \ 用于 bash 中的行继续,这是一个很好的做法,可以通过将每个选项放在自己的行中来打破本来很长的命令以更清楚地看到它。
  • \; 允许我们向原始 tmux 调用添加另一个 tmux 命令,而不是启动另一个 tmux 调用形式 bash
  • -d 到 运行 会话分离。如您所见,它的位置是灵活的,只要它在 new
  • 之后
  • new是别名,new-session
  • 的缩写

因此,当您 运行 脚本时,例如

$ ./script.sh

它悄悄地启动这些 tmux 会话。您可以检查它们是否存在:

$ tmux ls
test1: 1 windows (created Sun Mar 13 15:19:31 2016) [79x18]
test2: 1 windows (created Sun Mar 13 15:19:31 2016) [79x18]

并附加以查看它们,例如 test1:

$ tmux attach -t test1

除了其他选项外,您只需向每个 new 命令添加 -d 标志。

#!/bin/bash
tmux new -d -s test1 'mono --debug program1.exe'
tmux new -d -s test2 'python program2.py'

请注意,您现在有两个会话,您可以使用 tmux attach -s test1tmux attach -s test2 附加其中之一。在 相同 会话的单独 window 中 运行 每个命令可能更简单:

tmux new -d -s test1 'mono --debug program1.exe'
tmux new-window 'python program2.py'
tmux attach -t test1

new-window 替换为 split-window 以 运行 同一 window 中单独窗格中的命令。