【问题标题】:How to automatically terminate ssh connection after starting a script in tmux window?在 tmux 窗口中启动脚本后如何自动终止 ssh 连接?
【发布时间】:2018-03-05 19:18:49
【问题描述】:

我正在尝试使用 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分钟后)。如果我手动输入 control-C test_ssh.sh 停止并且 tmux -c test_tmux3.sh & 继续在计算机 $name 上运行(这就是我想要的)。如何自动化最后一步并让 ssh 自行退出?

【问题讨论】:

    标签: linux bash session ssh tmux


    【解决方案1】:

    在分离的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 命令就会退出。

    【讨论】:

      【解决方案2】:

      您是否尝试过使用 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 前面添加 nohup 并没有改变什么
      猜你喜欢
      • 2020-12-27
      • 2011-12-07
      • 2011-07-23
      • 1970-01-01
      • 2020-08-30
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多