From: Iain Patterson Date: Mon, 23 Apr 2007 15:53:47 +0000 (+0000) Subject: Handle ssh-agent socket going away. X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=c8d2eb023822ca4abf669b0380a806b3235c1d27;p=profile.git Handle ssh-agent socket going away. git-svn-id: https://svn.cambridge.iain.cx/profile/trunk@47 6be0d1a5-5cfe-0310-89b6-964be062b18b --- diff --git a/.profile.d/ssh.bashrc b/.profile.d/ssh.bashrc index 1e02b1c..5f3c1a5 100644 --- a/.profile.d/ssh.bashrc +++ b/.profile.d/ssh.bashrc @@ -2,6 +2,10 @@ SOCKET=~/.ssh/agent +function get_agent_pid() { + ps waux | grep -- ^$USER\ .\*ssh-agent\ -a\ .\*\ -s | grep -v grep | awk '{ print $2 }' | head -n 1 +} + if [ ! -z "$SSH_CLIENT" ]; then # Set display if we aren't already forwarding X11 if [ "$DISPLAY" = "" ]; then @@ -12,7 +16,17 @@ fi # Try to hook up with already running ssh-agent. if [ $UID -gt 0 ]; then # Not all OSes support ``ps -wu''. - RUNNING_AGENT="$(ps waux | grep -- ^$USER\ .\*ssh-agent\ -a\ .\*\ -s | grep -v grep | awk '{ print $2 }' | head -n 1)" + RUNNING_AGENT="$(get_agent_pid)" + + # Has the socket gone away? + if [ ! -z "$RUNNING_AGENT" -a ! -S "$SOCKET" ]; then + if kill $RUNNING_AGENT; then + RUNNING_AGENT="$(get_agent_pid)" + else + echo >&2 "$SOCKET has gone away but agent is running as PID $RUNNING_AGENT." + fi + fi + if [ -z "$SSH_AGENT_PID" -o ! "$SSH_AGENT_PID" = "$RUNNING_AGENT" ]; then export SSH_AGENT_PID="$RUNNING_AGENT" if [ "$SSH_AGENT_PID" = "" ]; then @@ -31,3 +45,4 @@ if [ $UID -gt 0 ]; then fi unset SOCKET +unset get_agent_pid