Skip to content

Instantly share code, notes, and snippets.

@skplunkerin
Created August 23, 2014 03:32
Show Gist options
  • Save skplunkerin/ff7622ff0812c5d42f73 to your computer and use it in GitHub Desktop.
Save skplunkerin/ff7622ff0812c5d42f73 to your computer and use it in GitHub Desktop.
auto-start ssh-agent on login
############ FROM http://rocksolidwebdesign.com/notes-and-fixes/ubuntu-server-ssh-agent/ ###########
# Check to see if SSH Agent is already running
agent_pid="$(ps -ef | grep "ssh-agent" | grep -v "grep" | awk '{print($2)}')"
# If the agent is not running (pid is zero length string)
if [[ -z "$agent_pid" ]]; then
# Start up SSH Agent
# this seems to be the proper method as opposed to `exec ssh-agent bash`
eval "$(ssh-agent)"
# If the agent is running (pid is non zero)
else
# Connect to the currently running ssh-agent
agent_ppid="$(($agent_pid - 1))"
# and the actual auth socket file name is simply numerically one less than
# the actual process id, regardless of what `ps -ef` reports as the ppid
agent_sock="$(find /tmp -path "*ssh*" -type s -iname "agent.$agent_ppid")"
echo "Agent pid $agent_pid"
export SSH_AGENT_PID="$agent_pid"
echo "Agent sock $agent_sock"
export SSH_AUTH_SOCK="$agent_sock"
fi
################ END SSH AGENT SCRIPT #################
@theavey
Copy link

theavey commented Jul 26, 2017

In my case, the ppid is not the pid minus one, but the ppid can be obtained in the same manner as the pid is found except getting the third element instead of the second.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment