服务状态健康检测

默北 Shell服务状态健康检测已关闭评论11,2191字数 357阅读1分11秒阅读模式

在大量服务器集群的环境下,有时某台服务器服务异常退出,导致一些访问请求出错。需要对当前的运行的服务进行检测,如有退出就自动启动。然而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/
weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 11/06/2012 14:30:11
  • 转载请务必保留本文链接:https://www.ttlsa.com/shell/check-server-1377/