如何检查 ssh-agent 是否已经在 bash 中运行? [英] How to check if ssh-agent is already running in bash?
问题描述
我的 Linux 环境中有一个示例 sh
脚本,它基本上运行的是当前 shell 的 ssh-agent
,向它添加一个密钥并运行两个 git 命令:
I have a sample sh
script on my Linux environment, which basically run's the ssh-agent
for the current shell, adds a key to it and runs two git commands:
#!/bin/bash
eval "$(ssh-agent -s)"
ssh-add /home/duvdevan/.ssh/id_rsa
git -C /var/www/duvdevan/ reset --hard origin/master
git -C /var/www/duvdevan/ pull origin master
脚本实际上工作得很好,但每次运行它时我都会得到一个新进程,所以我认为它可能会成为一个性能问题,最终我可能会有无用的进程.
Script actually works fine, but every time I run it I get a new process so I think it might become a performance issue and I might end up having useless processes out there.
输出示例:
Agent pid 12109
Identity added: /home/duvdevan/.ssh/custom_rsa (rsa w/o comment)
此外,除此之外,是否可以找到现有的 ssh-agent
进程并将我的密钥添加到其中?
Also, along with all this, is it possible to find an existing ssh-agent
process and add my keys into it?
推荐答案
不,真的,如何检查 ssh-agent
是否已经在 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...
这对我有用:
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
这篇关于如何检查 ssh-agent 是否已经在 bash 中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!