⚠️ Use keychain instead, since that will setup the rest for you.
Add following script to your .bashrc
or .zshrc
.
# this will load existing pid and socket variable
source ~/.sshagent > /dev/null
echo "[info] ssh-agent already started"
# if the socket is not available, then init ssh-agent
if [[ ! -S "$SSH_AUTH_SOCK" ]]; then
ssh-agent > ~/.sshagent
source ~/.sshagent > /dev/null
ssh-add
echo "[info] starting ssh-agent"
fi
NOTE: Windows is now have OpenSSH preinstalled. If you are using Windows, I recommend you to enable SSH Agent from services instead. Don't forget to setup Git to use external SSH.
For cygwin, check pid instead.
source ~/.sshagent > /dev/null
echo "[info] ssh-agent already started"
if [[ ! $(ps -sp $SSH_AGENT_PID | grep ssh-agent) ]]; then
ssh-agent > ~/.sshagent
source ~/.sshagent > /dev/null
ssh-add
echo "[info] starting ssh-agent"
fi