how to terminate ssh tunnel child process upon bash script exit
I found a way to do this using control sockets in SSH. Basically:
ssh -M -f -N -L $LOCAL_PORT:localhost:3306 $REMOTE_USER@$REMOTE_IP -S /tmp/ssh-controlmysql -P $LOCAL_PORT -h 127.0.0.1 -u lapl_stg -p${REMOTE_DB_PASS} < ./t1.sql > ./out.txtssh -S /tmp/ssh-control -O exit $REMOTE_IP
The reason why the process doesn't get killed at script exit is that -f
passed to ssh forks it to the background. This is needed for SSH tunnels to work without interrupting the execution of the next line.