简体   繁体   English

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

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

I'm trying to run a script in a tmux environment on another computer using ssh, but the ssh connection won't terminate until the script has finished. 我正在尝试在使用ssh的另一台计算机上的tmux环境中运行脚本,但是ssh连接不会终止,直到脚本完成为止。 Let me explain this in detail: 让我详细解释一下:

This is test_ssh.sh: 这是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

This is test_tmux3.sh (as a test to see if anything happens): 这是test_tmux3.sh(作为测试以查看是否发生任何事情):

#!/bin/bash

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

At the end I would like to loop over multiple computers ($name) to start a script on each of them. 最后,我想遍历多台计算机($ name)在每台计算机上启动一个脚本。 The problem I am having right now is that test_ssh.sh waits after the echo 1 and only exits after tmux -c test_tmux3.sh & is finished (after 2 minutes). 我现在遇到的问题是,test_ssh.sh在回声1之后等待,并且仅在tmux -c test_tmux3.sh&完成后(2分钟后)退出。 If I manually enter control-C test_ssh.sh stops and tmux -c test_tmux3.sh & continues running on the computer $name (which is what I want). 如果我手动输入control-C,则test_ssh.sh停止,并且tmux -c test_tmux3.sh&继续在计算机$ name上运行(这是我想要的)。 How can automate that last step and get ssh to exit on its own? 如何使最后一步自动化并让ssh自行退出?

Start the command in a detached tmux session. 在分离的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

Now, the tmux command will exit as soon as the new session is created and the script is started in that session. 现在,一旦创建新会话并在该会话中启动脚本, tmux命令将退出。

Have you tried to use nohup command to tell to the process keep running after exit?: 您是否尝试过使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM