问题
下载代码被墙
修改 hosts 文件
repo sync
时候卡住repo sync
时候中断
代码
要保证同时只有一个
repo
在运行
#!/bin/bash
# 当前 repo sync 进程的 pid
PID=
kill_all_prog() {
# 用ps找出所有的repo, 然后kill掉
echo "kill all prog(s)"
PIDS=`ps aux |grep python|grep [r]epo |awk '{print $2}'`
[[ -n $PIDS ]] && kill $PIDS
}
kill_prog() {
# kill 当前repo sync子进程
if [[ -n $PID ]]; then
echo "kill : $PID"
kill $PID
fi
}
start_sync() {
# 启动子进程(使用coproc)
coproc syncproc { repo sync; }
PID=$syncproc_PID
}
restart_sync() {
kill_prog
kill_all_prog
start_sync
}
# 如果网络流量在retry_delay时间内小于min_speed, 则认为repo sync已经卡住了
min_speed="50"
retry_delay=300
((counter=0))
((n_retries=0))
kill_all_prog
restart_sync
while [[ 1 ]]; do
# 用ifstat检测网速
speed=`ifstat 1 1 | tail -n 1 | awk '{print $1}'`
result=$(echo "$speed < $min_speed" | bc)
if [[ $result == "1" ]]; then
((counter++))
else
((counter=0))
fi
if [[ `ps -p $PID| wc -l` == "1" ]]; then
# 检测到子进程已经退出(ps已经查不到它了)
# 用wait取得子进程返回值
wait $PID
if [[ $? -eq 0 ]]; then
echo "sync successful"
break
else
echo "sync failed"
((counter=0))
((n_retries++))
restart_sync
continue
fi
fi
if ((counter > retry_delay)); then
((counter=0))
echo "netspeed low. restart!"
((n_retries++))
restart_sync
elif [[ counter -gt 20 ]]; then
echo -e "\n" "netspeed low counter is " $counter "\n";
fi
done
echo "completed with $n_retries retries"
废弃的代码
下面代码也是要保证只能有一个 repo
命令执行,且执行成功后还会执行
#!/bin/bash
# FIXME: 只允许同时一个repo运行
kill_prog() {
# 用ps找出所有的repo, 然后kill掉
PID=`ps aux |grep python|grep [r]epo |awk '{print $2}'`
[[ -n $PID ]] && kill $PID
}
start_sync() {
repo sync &
}
restart_sync() {
kill_prog
start_sync
}
# 如果网络流量在retry_delay时间内小于min_speed, 则认为repo sync已经卡住了
min_speed="50"
retry_delay=600
((counter=0))
((n_retries=0))
restart_sync
while [[ 1 ]]; do
# 用ifstat检测网速
speed=`ifstat 1 1 | tail -n 1 | awk '{print $1}'`
result=$(echo "$speed < $min_speed" | bc)
if [[ $result == "1" ]]; then
((counter++))
else
((counter=0))
fi
if ((counter > retry_delay)); then
((counter=0))
echo "netspeed low. restart!"
((n_retries++))
restart_sync
elif [[ counter -gt 10 ]]; then
echo -e "\n" "netspeed low counter is " $counter "\n";
fi
done
echo "completed with $n_retries retries"