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

Viewed 170

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

1 Answers

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

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