如何在 tmux window 中启动脚本后自动终止 ssh 连接?

How to automatically terminate ssh connection after starting a script in tmux window?

我正在尝试 运行 在另一台计算机上使用 ssh 的 tmux 环境中的脚本,但 ssh 连接在脚本完成之前不会终止。让我详细解释一下:

这是test_ssh.sh:

#!/bin/bash

name="computername"
ssh $name /bin/bash <<\EOF
    cd /scratch
    mkdir test
    cd test
    cp /home/user/test_tmux3.sh .
    tmux -c ./test_tmux3.sh &
    echo 1   # at this point it waits until test_tmux3.sh is finished, instead of exiting :(
EOF

这是test_tmux3.sh(作为测试以查看是否发生任何事情):

#!/bin/bash

mkdir 0min
sleep 60
mkdir 1min
sleep 60
mkdir 2min

最后我想遍历多台计算机($name)以在每台计算机上启动一个脚本。我现在遇到的问题是 test_ssh.sh 在 echo 1 之后等待,并且仅在 tmux -c test_tmux3.sh & 完成后(2 分钟后)退出。如果我在计算机 $name 上手动输入 control-C test_ssh.sh stops 和 tmux -c test_tmux3.sh & continues 运行ning (这是我想要的)。如何自动执行最后一步并让 ssh 自行退出?

您是否尝试过使用 nohup 命令告诉进程在退出后保持 运行?:

#!/bin/bash

name="computername"
ssh $name /bin/bash <<\EOF
    cd /scratch
    mkdir test
    cd test
    cp /home/user/test_tmux3.sh .
    nohup tmux -c ./test_tmux3.sh &
    echo 1   # at this point it waits until test_tmux3.sh is finished, instead of exiting :(
EOF

在分离的 tmux 会话中启动命令。

#!/bin/bash

name="computername"
ssh $name /bin/bash <<\EOF
    mkdir /scratch/test
    cd /scratch/test
    cp /home/user/test_tmux3.sh .
    tmux new-session -d ./test_tmux3.sh
    echo 1
EOF

现在,tmux 命令将在创建新会话并在该会话中启动脚本后立即退出。