Skip to content

Instantly share code, notes, and snippets.

@carlok
Created April 2, 2024 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlok/b9a14f1327777ffb017e75834657e8e8 to your computer and use it in GitHub Desktop.
Save carlok/b9a14f1327777ffb017e75834657e8e8 to your computer and use it in GitHub Desktop.
ssh-agent.sh
# grabbed from S.O. and then tuned it (sort of)
# SSH agent
ssh_pid_file="$HOME/.config/ssh-agent.pid"
SSH_AUTH_SOCK="$HOME/.config/ssh-agent.sock"
if [ -z "$SSH_AGENT_PID" ]
then
# no PID exported, try to get it from pidfile
SSH_AGENT_PID=$(cat "$ssh_pid_file")
fi
if ! kill -0 $SSH_AGENT_PID &> /dev/null
then
# the agent is not running, start it
rm "$SSH_AUTH_SOCK" &> /dev/null
>&2 echo "Starting SSH agent, since it's not running; this can take a moment"
eval "$(ssh-agent -s -a "$SSH_AUTH_SOCK")"
echo "$SSH_AGENT_PID" > "$ssh_pid_file"
ssh-add -A 2>/dev/null
>&2 echo "Started ssh-agent with '$SSH_AUTH_SOCK'"
# else
# >&2 echo "ssh-agent on '$SSH_AUTH_SOCK' ($SSH_AGENT_PID)"
fi
export SSH_AGENT_PID
export SSH_AUTH_SOCK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment