Ssh – Shell script to create background SSH connection if none exists

bashshellssh

There is a very similar question to this, but there is an extra detail I have in my situation.

Basically I am using a ControlMaster to speed up SSH connections. So I am using:

CurrentUser=$(whoami)
session=$(ps -el | grep $(id -u) |sed -e 's/sshd//g'| grep ssh)
if [ "$session" = "" ]
then
        ssh -TMNf synchro@129.130.155.30 &
fi

After this code I have another command. The purpose of the ssh command is solely to run in the background in order to keep an open socket for faster connections (if no calls occur for a short time then it times out and closes due to inactivity.)

The problem is this script doesn't close and return to the caller. It becomes a < defunct > process, and doesn't return until I manually kill the created ssh session. How can I let the script return to the caller and have the created ssh run in the background?

Edit: I want the created ssh session to persist after the script ends to keep a socket open for faster connections with an rsync command after this. With an open socket I go from 3-4 second transfer times to milliseconds

Best Answer

you want to work with the nohup command?