监控服务器端口,Down掉会自动重启,并发送邮件 Linux Shell
2015-11-30 17:59
736 查看
shell代码(注意,一定要在VIM中编辑,否则执行会有问题)
port=$1 if [ ! -n "$port" ];then echo "please enter the args port!" exit fi service_name=$2 nc -w2 localhost $port if [ $? != 0 ];then echo '【'`date +"%Y-%m-%d %H:%M:%S"`'】检测到端口【'${port}'】已经关闭,该服务重新启动'>>/root/monitor_log.log sh $2 exit fi
执行定时任务 每十秒执行一次
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.cn set 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=$1 if [ ! -n "$port" ];then echo "please enter the args port!" exit fi service_name=$2 nc -w2 localhost $port if [ $? != 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 exit fi
相关文章推荐
- linux Shell之cut命令用法
- win linux命令行下写webshell语句
- 让Linux下的cron以秒为单位执行shell脚本的3种方法
- shell知识点01
- Shell基础学习
- ShellSort
- shell 脚本
- powershell启用脚本
- SHELL编程入门(二)---SHELL循环与条件
- 本地代码调用远程shell代码
- shell命令中if语句的后街分号作用
- 如何确保 Shell 脚本只有一个实例运行
- shell 参数
- shell 参数
- shell 引号 转义符 通配符 特殊字符
- 利用Powershell快速导出域控所有用户Hash
- linux关于bashrc与profile的区别
- shell脚本中的$*,$@和$#
- linux shell date
- [Hadoop 2.6.0] Remote Submit Job Error:[ExitCodeException: /bin/bash: line 0: fg: no job control]