
在某些情況下,例如:
- 網路連線速度慢
- 緩慢的 PPA 或來源
- Wifi 上網或 3g 上網
apt-get 可能會在更新、安裝、升級或 dist-upgrade 過程中無休止地卡住(由您強制關閉)
當我說卡住時:它會下載文件,開始下載,放慢速度並在某個時刻等待,然後停止下載但仍在等待文件結尾。
據我了解,當存在大量延遲變化時(因此當伺服器飽和或有 wifi/3g 網路存取時),似乎會發生這種情況
此效果也會影響官方儲存庫。所以這不是 source.list 的事情。
我們如何告訴 apt-get :
- 停止無止盡的等待
- 下載過程中出現逾時或丟包時重試下載
我正在尋找一種不涉及暴力方法(例如Ctrl+C或kill)的解決方案。我正在尋找與腳本更相容的東西(因此當 apt-get 行啟動時沒有「人為」幹預)。
答案1
您可以使用timeout
命令(由同名軟體包安裝)來運行命令,如果需要超過氮秒。不過,我會小心何時使用它。在軟體包安裝過程中終止 apt-get 可能會造成混亂,因此我建議僅在超時情況下執行下載部分。像這樣的 bash 函數:
upgrade() {
local retry=5 count=0
# retry at most $retry times, waiting 1 minute between each try
while true; do
# Tell apt-get to only download packages for upgrade, and send
# signal 15 (SIGTERM) if it takes more than 10 minutes
if timeout -15 600 apt-get --assume-yes --download-only upgrade; then
break
fi
if (( count++ == retry )); then
printf 'Upgrade failed\n' >&2
return 1
fi
sleep 60
done
# At this point there should be no more packages to download, so
# install them.
apt-get --assume-yes upgrade
}
看如何運行命令並使其在 N 秒後中止(超時)?了解更多。
答案2
這是@geirha 答案的一般更新。
############ wrapper over apt-get to download files (retries if download fails) and then perform action.
############ usage example: aptgethelper install "nethogs rar -y -qq --force-yes"
function aptgethelper(){
local __cmd=$1
local __args=$2
local retry=10 count=0
set +x
# retry at most $retry times, waiting 1 minute between each try
while true; do
# Tell apt-get to only download packages for upgrade, and send
# signal 15 (SIGTERM) if it takes more than 10 minutes
if timeout --kill-after=60 60 apt-get -d $__cmd --assume-yes $__args; then
break
fi
if (( count++ == retry )); then
printf "apt-get download failed for $__cmd , $__args\n" >&2
return 1
fi
sleep 60
done
# At this point there should be no more packages to download, so
# install them.
apt-get $__cmd --assume-yes $__args
}