Last active
April 23, 2024 08:13
-
-
Save fenying/ec74418b7e94cd64862047948c62bb93 to your computer and use it in GitHub Desktop.
Auto-start ssh-agent when BASH starts, and reuse for all bash instance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add following code at the end of ~/.bashrc | |
# Check if ~/.pid_ssh_agent exists. | |
if [ -f ~/.pid_ssh_agent ]; then | |
source ~/.pid_ssh_agent | |
# Check process of ssh-agent still exists. | |
TEST=$(ssh-add -l) | |
if [ -z "$TEST" ]; then # Reinit if not. | |
NEED_INIT=1 | |
fi | |
else | |
NEED_INIT=1 # PID file doesm't exist, reinit it. | |
fi | |
# Try start ssh-agent. | |
if [ ! -z "$NEED_INIT" ]; then | |
echo $(ssh-agent -s) | sed -e 's/echo[ A-Za-z0-9]*;//g' > ~/.pid_ssh_agent # save the PID to file. | |
source ~/.pid_ssh_agent | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment