From da807b773fa9033015b1e14e2ee38834dd6224fe Mon Sep 17 00:00:00 2001 From: John Mertz Date: Mon, 13 Mar 2023 15:54:38 -0400 Subject: [PATCH] Detect existing socket, or not --- ssh-agent.sh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ssh-agent.sh b/ssh-agent.sh index 941a2b7..fba6859 100755 --- a/ssh-agent.sh +++ b/ssh-agent.sh @@ -1,3 +1,17 @@ #!/bin/bash +if [ -e $HOME/.spool/ssh-agent.sock ]; then + PID=$(tail -n 1 $HOME/.spool/ssh-agent.env | sed 's/echo Agent pid \([0-9]*\);/\1/') + if [ -z $PID ]; then + pkill ssh-agent + rm $HOME/.spool/ssh-agent.* + else + CMD=$(ps -p $PID -o comm | tail -n 1) + if [ ! -z $CMD ] && [ $CMD == 'ssh-agent' ]; then + echo "Valid agent already found at $HOME/.spool/ssh-agent.sock with PID $PID" + exit + fi + fi +fi + eval $(ssh-agent -a $HOME/.spool/ssh-agent.sock | tee $HOME/.spool/ssh-agent.env) >/dev/null