java进程监控,进程停止,自动启动并企微通知

一、编写监控通知脚本 

#!/bin/sh

jarDir="/app/back/sw-system"
jarName="sw-system-1.0.0-pro.jar"
logFile="/app/back/sw-system/autoMonitorRestart.log"

cd ${jarDir}
#查看进程是否存在
count=$(ps -ef | grep ${jarName} | grep -v "grep")
if [ -n "${count}" ];then
    echo "`date` ${jarName} is alive" >> ${logFile}
    #echo "`date` ${jarName} is alive"
else
    echo "`date` ${jarName} is dead. restart it" >> ${logFile}
    #echo "`date` ${jarName} is dead. restart it"
    #执行jar
    ./service.sh sw-system-1.0.0-p.jar restart
   
fi

 1.1企微通知脚本

 #企微请求通知(需要可以拷贝到上面去)

    phone="131********"
    content="监控到${jarName}挂掉了...自动重启中"
    json="{\"msgtype\":\"text\",\"text\":{\"content\":\"${content}\",\"mentioned_mobile_list\":\"${phone}\"}}"
     curl https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=12133 -X POST -d "${json}" --header "Content-Type: application/json"

二、centos增加定时任务 1、查询是否已安装crontabs,已安装跳过

yum install crontabs

tips:Errors during downloading metadata for repository 'AppStream':

- Status code: 404 for http://mirrors.cloud.aliyuncs.com/centos/8/AppStream/x86_64/os/repodata/repomd.xml (IP: 100.100.2.148)

阿里云yum仓库地址变更 

# 进入yum目录
cd /etc/yum.repos.d

# 删除目录下所有文件(注意完整复制,不要漏了那个点)
rm -rf ./*

# 安装正确的源
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo

# 生成缓存
yum makecache

2、安装corntabs

yum install crontabs

 安装完成

3、编辑或添加crontab任务

#查看已添加的定时任务: 

crontab -l

#两种方法修改或编辑:

crontab -e 
vi /etc/crontab

#每天凌晨4:00重启服务

0 4 * * * cd /app/java/ && ./service.sh demo1-SNAPSHOT.jar restart

#每分钟自动检查程序是否挂了,挂了自动重启

*/1 * * * * /app/back/sw-system/autoMonitorRestart.sh

表达式是否正确检测:在线crontab表达式执行时间计算工具_蛙蛙在线工具

4、重新载入配置

service crond reload 

5、其它命令

查看不同账号的 定时任务 crontab -u git -e

移除所有任务 crontab -r

查看状态:(systemctl) service status crond

 启动:(systemctl) service start crond

停止:(systemctl) service stop crond

重新载入配置:(systemctl) service crond reload 

开机启动:chkconfig crond on

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值