不知为何,PVE 中的 爱快 时不时会宕机,找了一个脚本,每隔一分钟检测一次虚拟机是否有心跳,如果没有心跳,就强制重启虚拟机。记录在这里。
#!/usr/bin/env bash
function check_and_restart() {
vm_id="${1}"
vm_ip="${2}"
# curl --connect-timeout 5 -sSL "${vm_ip}" > /dev/null
ping -c 1 "${vm_ip}" > /dev/null
if [[ $? != 0 ]]; then
now=`timedatectl status | grep 'Local time' | awk -F"Local time: " '{ print $2 }'`
echo "[${now}] [NO] id = ${vm_id}, ip = ${vm_ip}"
/usr/sbin/qm stop "${vm_id}"
/usr/sbin/qm start "${vm_id}"
fi
}
function main() {
vm_list=${1}
for each in ${vm_list}; do
vm_id=`echo "${each}" | awk -F: '{ print $1 }'`
vm_ip=`echo "${each}" | awk -F: '{ print $2 }'`
check_and_restart "${vm_id}" "${vm_ip}"
done
}
# 需要检查的虚拟机列表,格式为 vm_id:vm_ip
vm_list="
100:10.0.0.1
101:10.0.0.2
"
# 打印时间
# timedatectl status | grep 'Local time' | awk -F"Local time: " '{ print $2 }'
main "${vm_list}"
将以上文件保存至 /root/check_and_restart/check_and_restart.sh
添加定时任务
crontab -e
每隔一分钟检测一次
*/1 * * * * bash /root/check_and_restart/check_and_restart.sh >> /root/check_and_restart/log.txt
重启crontab服务
/etc/init.d/cron restart