前言
请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i、
提示:以下是本篇文章正文内容,下面案例可供参考
背景
当服务假死情况下,导致服务不可用,一时间定位不到服务假死原因,可以采用临时应急方案,需要增加探测机制,定时拉起重启服务
。
一、curl服务可用验证
注:
curl请求地址包含特殊字符时,需要将特殊字符转义,curl字符转义移步第四步
curl http://127.0.0.1:8088/GetResult.json?userName=superAdmin%26password=sadmin_secret_pw -d '{"id":100897}'
- 1
首先,正常运行情况下,适用下述curl指令可以正常返回结果:
二、服务探测脚本
编写shell脚本如下:
#!/bin/bash
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# + 1、此脚本为服务监控脚本,与系统定时任务配合使用
# + 2、执行crontab -e进入监控任务配置,配置探测脚本执行时间
# + 3、创建此脚本后请赋予可执行权限chmod +x chk_service.sh
# + 4、普通用户执行docker需要sudo 权限,在执行命令前先设置普通用户密码 !!!
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#获取当前时间
DATE=<span class="token hvariable">date</span> <span class="token operator">+%</span><span class="token constant">Y</span><span class="token operator">%</span><span class="token hvariable">m</span><span class="token operator">%</span><span class="token hvariable">d</span>
#检测地址
httpCode=<span class="token hvariable">curl</span> <span class="token operator">-</span><span class="token hvariable">s</span> <span class="token operator">-</span><span class="token hvariable">o</span> <span class="token operator">/</span><span class="token hvariable">dev</span><span class="token operator">/</span><span class="token builtin">null</span> <span class="token operator">-</span><span class="token hvariable">w</span> <span class="token string">"%{http_code}"</span> <span class="token operator">-</span><span class="token constant">X</span> <span class="token constant">POST</span> <span class="token operator">-</span><span class="token hvariable">d</span> '<span class="token punctuation">{<!-- --></span><span class="token string">"id"</span><span class="token operator">:</span><span class="token number">100897</span><span class="token punctuation">}</span>' <span class="token string">"http://127.0.0.1:8088/GetResult.json?userName=superAdmin&password=sadmin_secret_pw"</span>
#服务端口
ports=8088
#检查服务是否正常
function checkService()
{
echo "执行时间: \((date +%Y'-'%m'-'%d' '%H':'%M':'%S) "</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span> <span class="token string">"\)httpCode"=="200" ));then
echo ""
echo "检测服务正常运行服务可用!"
exit
else
echo ""
echo "检测服务不可用!"
echo "重启检测服务...预计两分钟"
echo "admin123" | sudo -S docker restart 09a380a14b7f
sleep 30s
<span class="token hvariable">status</span><span class="token operator">=</span>`<span class="token hvariable">netstat</span> <span class="token operator">-</span><span class="token hvariable">nltp</span><span class="token operator">|</span><span class="token hvariable">grep</span> <span class="token operator">$</span><span class="token punctuation">{<!-- --></span><span class="token hvariable">ports</span><span class="token punctuation">}</span><span class="token operator">|</span><span class="token hvariable">grep</span> <span class="token operator">-</span><span class="token hvariable">v</span> <span class="token string">"grep"</span>`
<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token operator">-</span><span class="token hvariable">z</span> <span class="token string">"${status}"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">then</span>
<span class="token hvariable">echo</span> <span class="token string">"docker检测服务重启失败..."</span>
<span class="token hvariable">echo</span> <span class="token string">"再次重启docker检测服务..."</span>
<span class="token hvariable">echo</span> <span class="token string">"admin123"</span> <span class="token operator">|</span> <span class="token hvariable">sudo</span> <span class="token operator">-</span><span class="token constant">S</span> <span class="token hvariable">docker</span> <span class="token hvariable">restart</span> 09a380a14b7f
<span class="token hvariable">exit</span>
<span class="token keyword">else</span>
<span class="token hvariable">echo</span> <span class="token string">"docker检测服务重启完成..."</span>
<span class="token hvariable">exit</span>
<span class="token hvariable">fi</span>
fi
}
checkService #调用函数
三、配置系统定时任务
监控脚本写完之后,便是把该脚本加入到crontab
监控任务中了。该过程可以去查看crontab
定时任务。我这个设置的是每一分钟执行一次,就是一行指令:先执行crontab -e
进入监控任务配置文件位置:
#查看监控任务
crontab -e
#配置任务
/1 * cd /root && sh cront_check.sh >> /root/cront.log
查看效果
四、Linux特殊字符转义
友情提示:
在url中直接使用特殊符号会截断命令让命令失去原本意义.,所以我们需要编码转义特殊符号
字符 | URL编码值 |
---|---|
空格 | %20 |
" | %22 |
# | %23 |
% | %25 |
& | %26 |
( | %28 |
) | %29 |
+ | %2B |
, | %2C |
/ | %2F |
: | %3A |
; | %3B |
< | %3C |
= | %3D |
> | %3E |
? | %3F |
@ | %40 |
\ | %5C |
| | %7C |
总结
我是南国以南i记录点滴每天成长一点点,学习是永无止境的!转载请附原文链接!!!