My issue is that we connect to our server and run a script. This generates 2 PIDs, 1 for the sshd and 1 for the bash. Occasionally the connection trips and the things we have created in the script remains.
We are trying to cleanup at the remote end, such that, if the sshd connection dies, then it automatically catches the signal and deletes the stuff we don't want. We are catching kill signals etc on the server, but we can't seem to catch a ssh drop.
It would not make sense to continue, when the sshd connection is lost, as we need the connection to be able to do the processing.
We tried by running 2 scripts, one that connects to the server and another that then does things.
Launcher
#!/bin/bash
#confirm arguments sent to server [args]
echo "You provided the arguments:" "$@"
#create ssh connection to remote server [args]
ssh -t "$1" 'trap : INT; bash -s' < monitor.sh "$2"
trap dockertermlocal EXIT
dockertermlocal()
{
echo "Caught termination signal from local!"
send clean up to remote server
echo "stopped docker from local"
exit 1
}
#Confirmation
echo "ran to completion"
Remote
#Command passed to script
echo "You provided the arguments:" "$@"
# Run docker command
eval "$@"
echo "ran command"
coproc ssh "$1" 'command & read; kill $!'