I have adopted the following snippet from Visual Studio Code's documentaion to create an ssh-agent on login:
if [ -z "$SSH_AUTH_SOCK" ]; then
# Check for a currently running instance of the agent
RUNNING_AGENT="`ps -ax | grep 'ssh-agent -s' | grep -v grep | wc -l | tr -d '[:space:]'`"
if [ "$RUNNING_AGENT" = "0" ]; then
# Launch a new instance of the agent
ssh-agent -s &> .ssh/ssh-agent
fi
eval `cat .ssh/ssh-agent`
fi
However, I see that on each login, a new ssh-agent is created when my ~/.zprofile is sourced, even if I have a few other sessions open.
While debugging the issue, I realized that a call to eval "$(ssh-agent -s)" creates the agent and prints its PID on the terminal. However, when I invoke ps, pgrep, htop, or similar commands, they do not show the ssh-agent process. If I rerun the same commands with sudo, I can find the process.
What can I do to make the ssh-agent process visible to the user who called it, so they can use the same agent in all their sessions?