How to know from a bash script if the user abruptly closes ssh session

Posted by Figo on Stack Overflow See other posts from Stack Overflow or by Figo
Published on 2010-05-13T13:59:21Z Indexed on 2010/05/13 14:04 UTC
Read the original article Hit count: 204

I have a bash script that acts as the default shell for a user loging in trough ssh. It provides a menu with several options one of wich is sending a file using netcat.

The netcat of the embedded linux I'm using lacks the -w option, so if the user closes the ssh connection without ever sending the file, the netcat command waits forever.

I need to know if the user abruptly closes the connection so the script can kill the netcat command and exit gracefully.

Things I've tried so far:

  • Trapping the SIGHUP: it is not issued. The only signal issued i could find is SIGCONT, but I don't think it's reliable and portable.
  • Playing with the -t option of the read command to detect a closed stdin: this would work if not for a silly bug in the embedded read command (only times out on the first invocation)

© Stack Overflow or respective owner

Related posts about bash

Related posts about shell-scripting