Problem
I was running a screen session remotely and got disconnected somehow. When I connect back in and issue my reconnect-to-screen command, the session hangs for a long time before connectiong.
Discussion
What appears to happen is that when screen receives the force-detach signal, it is trying to notify the tty that holds it that it is about to leave. Since the tty is hung due to a dropped connection it has to wait for the timeout to happen (which is upwards of five minutes in some cases).
Solution
This script looks for a screen session that you own, and kills the bash on that TTY. This lets screen complete the disconnect, and you can reconnect normally.
This script is very crude. It won't work properly in environments where you have multiple screen sessions going. It expects exactly one process owned by you with 'screen' in its name. So if you name this script 'killscreen', it won't work.
#!/bin/bash
if [ ! -z "$STY" ] ; then
echo Not a good idea to run this from inside screen
exit 0
fi
PTS=`ps -ef | grep screen | grep -v grep | grep $LOGNAME | awk '{print $6}'`
if [ ! -z "$PTS" ] ; then
PS=`ps -ef | grep $PTS | grep bash | awk '{print $2}'`
if [ ! -z "$PS" ] ; then
echo killing $PS
kill -9 $PS
screen -list
fi
fi