How to check if ssh-agent is already running in bash?
No, really, how to check if ssh-agent
is already running in bash?
Answers so far don't appear to answer the original question...
Here's what works for me:
if ps -p $SSH_AGENT_PID > /dev/null
then
echo "ssh-agent is already running"
# Do something knowing the pid exists, i.e. the process with $PID is running
else
eval `ssh-agent -s`
fi
This was taken from here
ssh-agent with passwords without spawning too many processes
have a look at Keychain. It was written b people in a similar situation to yourself.
Keychain
Start ssh-agent on login
Please go through this article. You may find this very useful:
https://web.archive.org/web/20210506080335/https://mah.everybody.org/docs/ssh
Just in case the above link vanishes some day, I am capturing the main piece of the solution below:
This solution from Joseph M. Reagle by way of Daniel Starin:
Add this following to your
.bash_profile
SSH_ENV="$HOME/.ssh/agent-environment"
function start_agent {
echo "Initialising new SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo succeeded
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
/usr/bin/ssh-add;
}
# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
#ps ${SSH_AGENT_PID} doesn't work under cywgin
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent;
fiThis version is especially nice since it will see if you've already started ssh-agent and, if it can't find it, will start it up and store the settings so that they'll be usable the next time you start up a shell.
Related Topics
How to Diff Top Lines of Two Files Without Intermediate File
Linking Boost Library in Linux
Null Modem Emulator (Com0Com) for Linux
How to Read the Last Line of a Text File into a Variable Using Bash
I Can't Execute Command Modprobe Vboxdrv
Calculating Rounded Percentage in Shell Script Without Using "Bc"
Mapping Physical Addresses to Virtual Address Linux
Codeigniter Url Rewriting .Htaccess Is Not Working on Centos
Putting Two Consecutive Lines into One Line with Perl/Awk
Pass Private Key Password to Openvpn Command Directly in Ubuntu 10.10
How to Configure the Linux Kernel Within Buildroot
How to Prevent Out of Memory (Oom) Freezes on Linux
Interrupting Epoll_Wait with a Non-Io Event, No Signals
What Unit Is Used to Display Redis CPU Usage