简体   繁体   中英

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. Let me explain this in detail:

This is 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):

#!/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. 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). 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). How can automate that last step and get ssh to exit on its own?

Start the command in a detached tmux session.

#!/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.

Have you tried to use nohup command to tell to the process keep running after exit?:

#!/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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