2

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 $!'
  • What OS? Systemd has features that would help, if you’re on an OS that uses it. – Wildcard Sep 23 at 17:24
  • Have you tried catching HUP? – ctrl-alt-delor Sep 23 at 17:32
  • how about put the wrapper at server side . If the wrapper find out its parent died , kill its children . notification can be done before cleaning – bigdataolddriver Sep 24 at 11:07
  • Out of interest, how did you catch sig-kill (the un-catchable signal). Do you mean sig-term (or another)? – ctrl-alt-delor Sep 24 at 18:27
2

I'm not sure about the other signals, but if there is communication back to the local machine, you can catch the PIPE signal there. When the connection is lost, the pipe will go down, and so the script will get the PIPE signal on write attempt. If there's not much communication, you can set there a background job that sends a character (say a dot) back to the initiating machine every second (or as often as you need), which will trigger a more or less immediate action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.