博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
监控服务器端口,Down掉会自动重启,并发送邮件 Linux Shell
阅读量:4077 次
发布时间:2019-05-25

本文共 1647 字,大约阅读时间需要 5 分钟。

shell代码(注意,一定要在VIM中编辑,否则执行会有问题)

port=$1if [ ! -n "$port" ];then        echo "please enter the args port!"        exitfiservice_name=$2nc -w2 localhost $portif [ $? != 0 ];then        echo '【'`date +"%Y-%m-%d %H:%M:%S"`'】检测到端口【'${port}'】已经关闭,该服务重新启动'>>/root/monitor_log.log        sh $2	exitfi

执行定时任务 每十秒执行一次

crontab -e* * * * * sleep 10; /root/port_monitor.sh 90  /var/dbstar/program/dmail-tomcat/bin/startup.sh

添加邮件功能

若想使用shell中的第14行代码,需要事先安装邮件服务。安装步骤如下:
vim /etc/mail.rc
在最后添加以下两行代码
set from=xxx@xxx.com.cn smtp=mail.xxx.com.cnset smtp-auth-user=xxx@xxx.com.cn smtp-auth-password=123456 smtp-auth=login
发送邮件命令,添加到第14行(shell)
echo "the port ${port} is down,the server is  restart ${port} port!!"|mail -s "port ${port}/${service_name} is down!" xxx@xxx.com.cn

启动tomcat时乱码解决方案

在catalina.sh中添加如下代码即可

JAVA_OPTS="-Dfile.encoding=utf-8"

最终代码(shell)

port=$1if [ ! -n "$port" ];then        echo "please enter the args port!"        exitfiservice_name=$2nc -w2 localhost $portif [ $? != 0 ];then        echo '【'`date +"%Y-%m-%d %H:%M:%S"`'】检测到端口【'${port}'】已经关闭,该服务重新启动'>>/root/monitor_log.log        if [ -n "$service_name" ];then                sh $2        fi        if [ ! -n "$service_name" ];then                mkdir /tmp/portmonitor/ -p                touch /tmp/portmonitor/${port}.log                flag=$(cat /tmp/portmonitor/${port}.log)                if [ ! -n "$flag" ] || [  "1" != "$flag" ];then                        echo '1'>/tmp/portmonitor/${port}.log                fi        fi        echo "the port ${port} is down,the server is  restart ${port} port!!"|mail -s "port ${port}/${service_name} is down!" xxx@xxx.com.cn        exitfi

转载地址:http://ainni.baihongyu.com/

你可能感兴趣的文章
IE不支持option的display:none属性
查看>>
[分享]mysql内置用于字符串型ip地址和整数型ip地址转换函数
查看>>
TableDnd(JQuery表格拖拽控件)应用进阶
查看>>
[转]开源中最好的Web开发的资源
查看>>
Docker上部署SpringBoot项目并推送镜像到Docker Hub上---以MacOS为例
查看>>
bibtex I was expecting a `,‘ or a `}‘ 问题解决
查看>>
sql server中各类范式的理解
查看>>
Python中列表元素删除
查看>>
二分查找与递归式二分查找
查看>>
在Navicat for MySQL中修改表的编码格式
查看>>
补充另一版ArrayList的初始化过程
查看>>
java接口不能实例化原因浅谈
查看>>
Https加密及攻防
查看>>
Java生成随机不重复推广码邀请码
查看>>
Java8 Lambda表达式介绍
查看>>
Java NIO源码剖析及使用实例(一):Buffer
查看>>
【JAVA数据结构】双向链表
查看>>
【JAVA数据结构】先进先出队列
查看>>
String类的intern方法随笔
查看>>
【泛型】一个简易的对象间转换的工具类(DO转VO)
查看>>