在大量服务器集群的环境下,有时某台服务器服务异常退出,导致一些访问请求出错。需要对当前的运行的服务进行检测,如有退出就自动启动。然而cron任务最小频率在分钟级别,这显然有点长。下面这个脚本是在秒级对服务进行健康检测。
[codesyntax lang="bash"]文章源自运维生存时间-https://www.ttlsa.com/shell/check-server-1377/
#!/bin/bash文章源自运维生存时间-https://www.ttlsa.com/shell/check-server-1377/
i=0文章源自运维生存时间-https://www.ttlsa.com/shell/check-server-1377/
interval=3文章源自运维生存时间-https://www.ttlsa.com/shell/check-server-1377/
count=$[60/$interval] # $[]只能进行整数运算文章源自运维生存时间-https://www.ttlsa.com/shell/check-server-1377/
while [ $i -lt $count ]文章源自运维生存时间-https://www.ttlsa.com/shell/check-server-1377/
do文章源自运维生存时间-https://www.ttlsa.com/shell/check-server-1377/
nginx_arr=(`pgrep nginx`)文章源自运维生存时间-https://www.ttlsa.com/shell/check-server-1377/
if [ ${#nginx_arr[@]} == 0 ]; then文章源自运维生存时间-https://www.ttlsa.com/shell/check-server-1377/
nginx -c /usr/local/nginx/etc/nginx.conf >/dev/null 2>&1文章源自运维生存时间-https://www.ttlsa.com/shell/check-server-1377/
fi文章源自运维生存时间-https://www.ttlsa.com/shell/check-server-1377/
((i++))文章源自运维生存时间-https://www.ttlsa.com/shell/check-server-1377/
sleep $interval文章源自运维生存时间-https://www.ttlsa.com/shell/check-server-1377/
done文章源自运维生存时间-https://www.ttlsa.com/shell/check-server-1377/
[/codesyntax]文章源自运维生存时间-https://www.ttlsa.com/shell/check-server-1377/
文章源自运维生存时间-https://www.ttlsa.com/shell/check-server-1377/文章源自运维生存时间-https://www.ttlsa.com/shell/check-server-1377/
评论