背景:
定时监控应用服务端口是否正常,端口不存在表示服务已停止,需要重启服务。
以webSocket服务,端口9086为例,shell脚本写法为:
#!/bin/bash
#监控网关服务是否开启
port=`netstat -an | grep ":9086"|wc -l`
if [ $port -ne 1 ]
then
$(cd /usr/local/services/apache-tomcat-8.0.33/BTSGateway-9086/bin && /bin/sh startup.sh)
else
echo "gateway is running"
fi
rz命令上传到linux服务器后执行格式化:dos2unix testStart.sh
设置定时任务:crontab -e 按insert 进入编辑模式 输入如下命令: 指定日期及shell脚本
0 2 * * * /bin/bash /usr/local/services/apache-tomcat-8.0.33/shell/typeCount/testStart.sh
:wq 保存并退出
shell 跑数命令
切换到对应服务器:
chmod +x ./testStart.sh 给脚本添加执行权限
./testStart.sh 在脚本当前目录下执行脚本
创建shell 脚本:crontab -e 按insert 进入编辑模式 输入如下命令: 指定日期及shell脚本
0 2 * * * /bin/bash /usr/local/services/apache-tomcat-8.0.33/shell/typeCount/testStart.sh
:wq 保存并退出
查看shell脚本:crontab -l