当前位置:网站首页>监听服务器jar运行,及重启脚本
监听服务器jar运行,及重启脚本
2022-07-27 18:56:00 【捡黄金的少年】
1、简单监听服务并重启脚本
业务需求,服务器要监听停止的jar服务,十分钟一次监听,出现停止就重启服务
#!/bin/bash
# “#!” 是一个约定的标记,它告诉系统这个脚本需要什么解释器来执行
# 变量
jarName=quartz_cgq_5501.jar
jarName_TWO=quartz_cgq_5506.jar
jarName_THREE=suoxin_quartz_cgq_5503.jar
#配置服务日志文件目录路径
LOG_FILE=/usr/local/kuangshan/jiaoben/quartz_cgq_5501.log
LOG_FILE_TWO=/usr/local/kuangshan/jiaoben/quartz_cgq_5506.log
LOG_FILE_THREE=/usr/local/kuangshan/jiaoben/suoxin_quartz_cgq_5503.log
jarUrl=/usr/local/kuangshan/
#webUrl=https://www.baidu.com/
# grep -v grep: 在文档中过滤掉包含有grep字符的行
# awk '{print $2}': 按空格截取第二个
#pid=`ps -ef|grep $jarName |grep -v grep| awk '{print $2}'`
# wc -l: jar包进程的数量
num=`ps -ef|grep $jarName |grep -v grep| awk '{print $2}'| wc -l`
MonitorFunction(){
# 输出文本
echo "[info]进入监控脚本"`date +'%Y-%m-%d %H:%M:%S'`
# -eq: 等于
if [[ $num -eq 0 ]]; then
echo "[error]进程一,不存在,重启"`date +'%Y-%m-%d %H:%M:%S'`
nohup java -jar $jarUrl/$jarName > $LOG_FILE 2>&1 &
else
echo "[info]页面一,存在不用重启,time="`date +'%Y-%m-%d %H:%M:%S'`
fi
}
MonitorFunction
numTwo=`ps -ef|grep $jarName_TWO |grep -v grep| awk '{print $2}'| wc -l`
MonitorFunctionTwo(){
# 输出文本
echo "[info]进入监控脚本二"`date +'%Y-%m-%d %H:%M:%S'`
# -eq: 等于
if [[ $numTwo -eq 0 ]]; then
echo "[error]进程二,不存在,重启"`date +'%Y-%m-%d %H:%M:%S'`
# >> /dev/null : jar包生成日志不打印到此脚本日志中
nohup java -jar $jarUrl/$jarName_TWO > $LOG_FILE_TWO 2>&1 &
else
echo "[info]页面二,存在不用重启,time="`date +'%Y-%m-%d %H:%M:%S'`
fi
}
MonitorFunctionTwo
numThree=`ps -ef|grep $jarName_THREE |grep -v grep| awk '{print $2}'| wc -l`
MonitorFunctionThree(){
# 输出文本
echo "[info]进入监控脚本三"`date +'%Y-%m-%d %H:%M:%S'`
# -eq: 等于
if [[ $numThree -eq 0 ]]; then
echo "[error]进程三,不存在,重启"`date +'%Y-%m-%d %H:%M:%S'`
# >> /dev/null : jar包生成日志不打印到此脚本日志中
nohup java -jar $jarUrl/$jarName_THREE > $LOG_FILE_THREE 2>&1 &
else
echo "[info]页面三存在不用重启,time="`date +'%Y-%m-%d %H:%M:%S'`
fi
}
MonitorFunctionThree
2、凌晨重启jar服务脚本
#!/bin/bash
#执行bash shell配置
source ~/.bash_profile
#配置服务jar包文件路径
SERVICE_NAME=/usr/local/kuangshan/quartz_cgq_5501.jar
SERVICE_NAME_TWO=/usr/local/kuangshan/quartz_cgq_5506.jar
SERVICE_NAME_THREE=/usr/local/kuangshan/suoxin_quartz_cgq_5503.jar
#配置服务日志文件目录路径
LOG_FILE=/usr/local/kuangshan/jiaoben/quartz_cgq_5501.log
LOG_FILE_TWO=/usr/local/kuangshan/jiaoben/quartz_cgq_5506.log
LOG_FILE_THREE=/usr/local/kuangshan/jiaoben/suoxin_quartz_cgq_5503.log
#杀死指定jar包进程
ps -ef|grep $SERVICE_NAME |grep -v grep|cut -c 9-15|xargs kill -9
ps -ef|grep $SERVICE_NAME_TWO |grep -v grep|cut -c 9-15|xargs kill -9
ps -ef|grep $SERVICE_NAME_THREE |grep -v grep|cut -c 9-15|xargs kill -9
#启动jar包
nohup java -jar -Xmx10240m -Xms10240m -Xss2m $SERVICE_NAME > $LOG_FILE 2>&1 &
nohup java -jar -Xmx10240m -Xms10240m -Xss2m $SERVICE_NAME_TWO > $LOG_FILE_TWO 2>&1 &
nohup java -jar -Xmx10240m -Xms10240m -Xss2m $SERVICE_NAME_THREE > $LOG_FILE_THREE 2>&1 &
echo "服务重启时间:"`date`>>restart.out
3、先测试脚本是否能运行
给脚本赋权
chmod a+x monitor.sh
运行脚本文件
sh monitor.sh
如果出现下面错误,则输入命令,转化为unix格式
sed -i 's/\r//' monitor.sh

3、使用crontab的定时任务
crontab -e
0 0 * * *表示凌晨执行
*/10 * * * * 表示十分钟执行一次
0 0 * * * bash /usr/local/kuangshan/jiaoben/springStart.sh >> /usr/local/kuangshan/jiaoben/log.log
*/10 * * * * bash /usr/local/kuangshan/jiaoben/monitor.sh >> /usr/local/kuangshan/jiaoben/logTWO.log
查看编辑的定时任务
crontab -l
边栏推荐
- 用伪元素before实现元素的等比例缩放
- What are the practical advantages of digital factory system
- Puzzle (002) inner solid, outer solid, Hamilton
- Leetcode daily practice - 203. remove linked list elements
- PostgreSQL source code (65) analysis of the working principle of globalvis, a new snapshot system
- Guava Cache 原理分析与最佳实践
- Understanding network model overview of network model
- ZABBIX monitoring service (III) configuration management graphics and windows
- Uncaught SyntaxError: redeclaration of let page
- Unity 安装个人免费版
猜你喜欢

多人协作开发规范

激光雷达中国前装大幕开启,数百万颗产能待消化

使用百度飞桨EasyDL实现电商UGC图片自动分类

Chinese and English instructions - abfluor 488 cell apoptosis detection kit

Worthington plasma amine oxidase (PAO) instructions

ADB shell LS /system/bin (index table)

A new technical director asked me to do an IP territorial function~

Can single mode and multi-mode of industrial switches replace each other?

Worthington phospholipase A2 study phosphatidylcholine 2-acetylhydrolase

Uncaught SyntaxError: redeclaration of let page
随机推荐
Understand the communication mode of transmission media
Simple use of express web server
Principle analysis and best practice of guava cache
“地理-语言”大模型文心ERNIE-GeoL及应用
puzzle(002)内固、外固、哈密顿
Understanding of reg type variables in Verilog HDL
30分钟彻底弄懂 synchronized 锁升级过程
Unity 安装个人免费版
成分句法分析综述(第二版)
【华为HCIE安全考什么科目?华为HCIE安全考什么知识点?】
激光雷达中国前装大幕开启,数百万颗产能待消化
MobileVIT学习笔记
建筑云渲染的应用正在扩大,越来越多的行业急需可视化服务
Remember that resttemplate.getforentity failed to carry headers once, resttemplate exchange
Process management process monitoring and management ps/pstree/top/lsof
Win11小组件提示加载此内容时出现错误怎么解决?
[what subjects does Huawei hcie security test? What knowledge points does Huawei hcie security test have?]
Common ArrayList interview questions
飞桨框架体验评测交流会,产品的使用体验由你来决定!
The new CTO strongly prohibits the use of calendar?