如何检查ssh-agent是否已在bash中运行? [英] How to check if ssh-agent is already running in bash?

查看:368
本文介绍了如何检查ssh-agent是否已在bash中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux环境中有一个示例sh脚本,该脚本基本上针对当前shell运行ssh-agent,向其添加密钥并运行两个git命令:

#!/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

脚本实际上可以正常运行,但是每次运行它时,我都会得到一个新进程,因此我认为它可能会成为性能问题,并且最终可能会导致无用的进程.

输出示例:

Agent pid 12109
Identity added: /home/duvdevan/.ssh/custom_rsa (rsa w/o comment)

此外,是否还有可能找到现有的ssh-agent进程并将我的密钥添加到其中?

解决方案

否,真的,如何检查ssh-agent是否已在bash中运行?

到目前为止,答案似乎并未回答原始问题...

这是对我有用的:

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

摘录自: https://stackoverflow.com/a/15774758

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.

An example of the output:

Agent pid 12109
Identity added: /home/duvdevan/.ssh/custom_rsa (rsa w/o comment)

Also, along with all this, is it possible to find an existing ssh-agent process and add my keys into it?

解决方案

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: https://stackoverflow.com/a/15774758

这篇关于如何检查ssh-agent是否已在bash中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆