Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

bash - How to drop ssh session in shell? ssh keeps session alive even after script exits

How to drop ssh session in an automation script write in bash? I have a script running local to trigger a script on a remote machine, and the script on remote machine will trigger another script running in the background... I want to drop the session while keep the remote machine still running the background script, so I use nohup.

I have a local script localScript as follows

#!/bin/bash
echo "start remote trigger script..."
./trigger

the trigger script is ready on my remote machine with the following lines:

#!/bin/bash
echo "start script test..."
nohup ./test > output &
echo "start test script in background, exit..."
exit

The test script is a basic sleep loop just for testing...

#!/bin/bash
c=1
while [ "$c" -le 10 ]
do
        echo "sleep 10 seconds, c=$c"
        sleep 10s
        c=$((c+1))
        if [ "$c" -eq 10 ]
        then
                echo "max count reach, exit"
                exit
        fi
done

But what I found is the ssh keeps session alive (wait idle for 100 seconds), how can I drop the session? The command I use is sshpass -p XXXX ssh -o StrictHostKeyChecking=no user@IP 'bash -s' < localScript

question from:https://stackoverflow.com/questions/65617528/how-to-drop-ssh-session-in-shell-ssh-keeps-session-alive-even-after-script-exit

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This should work if you force pseudo-terminal allocation (-t):

sshpass -p XXXX ssh -t -o StrictHostKeyChecking=no user@IP 'bash -s' < localScript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...