2017年的迪士尼项目,运行了差不多2年了,其中比较大的问题有两点

  1. socket日志比较大,当时每个考虑是保存最近7天的,对日志没有进行按天保存,导致半年后日志过大,需要手动ssh到服务器删除日志
  2. API服务,差不多也会在半年挂一次,登录服务器上开端口还在,服务没挂,但是就是API访问失败
    针对上面两点,我们需要在Linux服务器上添加两个定时任务,主要是使用crontab命令

API挂掉原因,目测是flask的原因,没有设置超时,还有就是运营商的问题

1
2
3
4
root@iZ2zecs04g3wrhai0d115qZ:~/crontab# lsof -i:801
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python 28053 root 4u IPv4 10288077 0t0 TCP *:801 (LISTEN)
python 28053 root 145u IPv4 10293251 0t0 TCP 192.168.1.189:801->xxxxxxx:10907 (ESTABLISHED)

编写脚本

crontab定时任务添加

执行crontab -e编辑如下

1
2
3
4
# 每5分钟执行一次
*/5 * * * * /root/crontab/iotAPIMonitor.sh
# 每周执行一次
0 0 * * 0 /root/crontab/clearLog.sh

编写API监听脚本iotAPIMonitor.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/env bash
source /etc/profile
source ~/.bashrc
workdir=$(cd $(dirname $0); pwd)
# 10s中超时
result=`curl --connect-timeout 10 -m 20 "http://127.0.0.1:xxxx"`
echo "result=${result}"
echo `pwd`
echo ${workdir}
if test "${result}" = "404"; then
echo "server alive"
else
echo "server dead"
cd ${workdir}
mailx -s "服务器挂了,请去手动重启" xxxx@gmail.com xxxx@163.com xxxx1@163.com < message.txt
echo "end start"
fi

日志清零脚本clearLog.sh

1
2
3
4
5
#!/usr/bin/env bash
cd /root/iot
rm -rf iot.log
rm -rf app.log
rm -rf server.log

邮件内容message.txt

请在/root/iot目录下执行
./kill.sh
./run.sh
若还没效果,请重启服务器后,执行上述命令

注意点,自己的环境变量在crontab不起作用,需要source一下自己添加的环境变量

邮件配置相关

生成cert文件

cert.sh

1
2
3
4
5
6
7
8
9
apt install libnss3-tools
mkdir .certs
echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > .certs/qq.crt
certutil -A -n "GeoTrust SSL CA" -t "C,," -d .certs -i .certs/qq.crt
certutil -A -n "GeoTrust Global CA" -t "C,," -d .certs -i .certs/qq.crt
certutil -L -d .certs

cd .certs
certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt

配置/etc/s-nail.rc

1
2
3
4
5
6
7
8
9
set from=xxxxxxxx@qq.com
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=xxxxxxx@qq.com
# 这个去开启QQ邮箱的POP3会有一串码
set smtp-auth-password=xxxxxxxx
set smtp-auth=login
set ssl-verify=ignore
# 上面生成的cert.sh文件路径
set nss-config-dir=/root/crontab/.certs