我使用 bash 腳本 (deploy.sh) 將我的應用程式部署到共享主機。作為部署程序的一部分,我使用以下腳本從 bitbucket 克隆最新代碼:
eval `ssh-agent -s`
ssh-add ~/.ssh/SHA256-XXX.priv
git clone [email protected]:username/gng2.git --branch $branchname --single-branch
似乎這個腳本在共享主機上導致了很多“死”行程,當我達到限制時,我的應用程式不再作業,因為沒有更多的空閑行程。請參閱下面的一些死行程示例:
699 65313 0.0 0.0 7112 1752 ? Ss Jan04 0:00 ssh-agent -s
699 67925 0.0 0.0 7112 1744 ? Ss Feb07 0:00 ssh-agent -s
699 70469 0.0 0.0 7112 1612 ? Ss Jan04 0:00 ssh-agent -s
699 71078 0.0 0.0 7112 2352 ? Ss Feb10 0:00 ssh-agent -s
托管公司的支持團隊幫助追蹤到死行程是由我的部署腳本啟動的:
[email protected]:~$ grep -ril "ssh-agent" .
./www/example.com/gng2-core/vendor/phpseclib/phpseclib/phpseclib/Net/SSH2.php
./www/example.com/gng2-core/vendor/phpseclib/phpseclib/phpseclib/System/SSH/Agent/Identity.php
./www/example.com/gng2-core/vendor/phpseclib/phpseclib/phpseclib/System/SSH/Agent.php
./www/example.com/repos/gng2/deploy/scripts/deploy.sh
./www/example.com/repos/deploy.sh
本文建議我的腳本可能“生成一個獨特的 ssh-agent 實體,即使在注銷后,它仍會使用記憶體中添加的密鑰運行,除非明確終止”
我如何需要更改我的腳本,以便它不會創建這么多(死)行程?我可以簡單地將以下內容添加到腳本的末尾來解決這個問題嗎?
eval `ssh-agent -k`
或者這個問題有更好的解決方案嗎?
uj5u.com熱心網友回復:
您的腳本可能不應該啟動ssh-agent;它應該利用一個ssh-agent已經在運行的。這樣,用戶負責啟動可由腳本的多次呼叫使用的單個代理。
但是,您可以做的最簡單的事情就是添加
kill $SSH_AGENT_PID
要么
ssh-agent -k
到你的腳本結束來殺死剛剛啟動的代理。該命令所做的一件事eval是將 的值設定為SSH_AGENT_PID剛剛啟動的代理的行程 ID。
(如果您出于某種原因有多個并發代理,則前者很有用,以便您殺死正確的代理。)
uj5u.com熱心網友回復:
必須仔細處理多個方面的問題:
首先檢查 ssh-agent 不可用
#!/usr/bin/env bash
# Limit risk of leaving script with an ssh key unlocked
# Keep in mind that SIGKILL cannot be trapped, so it is not 100% abuse-proof
# So remove all pending authorizations
trap 'ssh-add -D 2>/dev/null"' EXIT INT
# Spawn an ssh agent for a limited time only if none already available
if [ -z "$SSH_AUTH_SOCK" ] && [ -z "$SSH_AGENT_PID" ] ! ; then
# Runs the agent only for the next 5 minutes
eval "$(ssh-agent -t 600)"
fi
# Now that we know an ssh-agent is available
# we can register our ssh key with an expiration timeout
# so the authentication does not remain exposed for too long
ssh-add -t 600 # expire in 5 minutes
關于使用時的安全注意事項ssh-agent:
https://goteleport.com/blog/how-to-use-ssh-agent-safely/
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/432625.html
上一篇:如何洗掉游標后的所有內容?
