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
# 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
fi
unset SOCKET
+unset get_agent_pid