linux编写脚本监控springboot项目是否挂掉,如挂掉则重启

linux编写脚本监控springboot项目是否挂掉,如挂掉则重启

shell脚本内容如下:

jarname='hdfsdownload-0.0.1-SNAPSHOT'
pid=`ps aux | grep $jarname | grep -v grep | awk '{print $2}'`
echo $pid
if [ ! $pid ]; then
echo '项目重新启动' >/bigdata/run.log &
source /etc/profile
nohup java -jar /bigdata/$jarname.jar  >/bigdata/run.log &
else
echo "项目已经启动"  >/bigdata/run.log &
fi

下面对每一行做出解释:

jarname='hdfsdownload-0.0.1-SNAPSHOT'

将jar包名定义为一个变量

pid=`ps aux | grep $jarname | grep -v grep | awk '{print $2}'`

根据包名查询该项目的进程号

echo $pid

输出进程号

if [ ! $pid ]; then

如果进程号为空

echo '项目重新启动' >/bigdata/run.log &

将提示信息输出到指定日志文件,目的是做个标记

source /etc/profile

设置环境变量,因为定时器执行脚本的环境跟你直接执行是不一样的,所以必须设置环境变量。

nohup java -jar /bigdata/$jarname.jar  >/bigdata/run.log &

在后台启动springboot项目,并将日志输出到/bigdata/run.log日志文件

else
echo "项目已经启动"  >/bigdata/run.log &

将提示信息输出到/bigdata/run.log日志文件,这句只是为了标志一下,如脚本成功可以用#注释掉

fi

下面就是设置定时任务,如没有则用以下命令进行安装和启动:
安装:

yum -y install vixie-cron
yum -y install crontabs

启动:

service crond start     //启动服务
service crond stop      //关闭服务
service crond restart   //重启服务
service crond reload    //重新载入配置
service crond status    //查看crontab服务状态

设置定时任务:

crontab -e

你会进入一个定时任务文件,输入以下命令设置定时任务:
每分钟执行脚本文件:

*/1 * * * * sh  /bigdata/cron.sh&

每秒钟执行脚本文件:

* * * * * sh /bigdata/cron.sh&
* * * * * sleep 1; sh /bigdata/cron.sh&
* * * * * sleep 2; sh /bigdata/cron.sh&
* * * * * sleep 3; sh /bigdata/cron.sh&
* * * * * sleep 4; sh /bigdata/cron.sh&
* * * * * sleep 5; sh /bigdata/cron.sh&
* * * * * sleep 6; sh /bigdata/cron.sh&
* * * * * sleep 7; sh /bigdata/cron.sh&
* * * * * sleep 8; sh /bigdata/cron.sh&
* * * * * sleep 9; sh /bigdata/cron.sh&
* * * * * sleep 10; sh /bigdata/cron.sh&
* * * * * sleep 11; sh /bigdata/cron.sh&
* * * * * sleep 12; sh /bigdata/cron.sh&
* * * * * sleep 13; sh /bigdata/cron.sh&
* * * * * sleep 14; sh /bigdata/cron.sh&
* * * * * sleep 15; sh /bigdata/cron.sh&
* * * * * sleep 16; sh /bigdata/cron.sh&
* * * * * sleep 17; sh /bigdata/cron.sh&
* * * * * sleep 18; sh /bigdata/cron.sh&
* * * * * sleep 19; sh /bigdata/cron.sh&
* * * * * sleep 20; sh /bigdata/cron.sh&
* * * * * sleep 21; sh /bigdata/cron.sh&
* * * * * sleep 22; sh /bigdata/cron.sh&
* * * * * sleep 23; sh /bigdata/cron.sh&
* * * * * sleep 24; sh /bigdata/cron.sh&
* * * * * sleep 25; sh /bigdata/cron.sh&
* * * * * sleep 26; sh /bigdata/cron.sh&
* * * * * sleep 27; sh /bigdata/cron.sh&
* * * * * sleep 28; sh /bigdata/cron.sh&
* * * * * sleep 29; sh /bigdata/cron.sh&
* * * * * sleep 30; sh /bigdata/cron.sh&
* * * * * sleep 31; sh /bigdata/cron.sh&
* * * * * sleep 32; sh /bigdata/cron.sh&
* * * * * sleep 33; sh /bigdata/cron.sh&
* * * * * sleep 34; sh /bigdata/cron.sh&
* * * * * sleep 35; sh /bigdata/cron.sh&
* * * * * sleep 36; sh /bigdata/cron.sh&
* * * * * sleep 37; sh /bigdata/cron.sh&
* * * * * sleep 38; sh /bigdata/cron.sh&
* * * * * sleep 39; sh /bigdata/cron.sh&
* * * * * sleep 40; sh /bigdata/cron.sh&
* * * * * sleep 41; sh /bigdata/cron.sh&
* * * * * sleep 42; sh /bigdata/cron.sh&
* * * * * sleep 43; sh /bigdata/cron.sh&
* * * * * sleep 44; sh /bigdata/cron.sh&
* * * * * sleep 45; sh /bigdata/cron.sh&
* * * * * sleep 46; sh /bigdata/cron.sh&
* * * * * sleep 47; sh /bigdata/cron.sh&
* * * * * sleep 48; sh /bigdata/cron.sh&
* * * * * sleep 49; sh /bigdata/cron.sh&
* * * * * sleep 50; sh /bigdata/cron.sh&
* * * * * sleep 51; sh /bigdata/cron.sh&
* * * * * sleep 52; sh /bigdata/cron.sh&
* * * * * sleep 53; sh /bigdata/cron.sh&
* * * * * sleep 54; sh /bigdata/cron.sh&
* * * * * sleep 55; sh /bigdata/cron.sh&
* * * * * sleep 56; sh /bigdata/cron.sh&
* * * * * sleep 57; sh /bigdata/cron.sh&
* * * * * sleep 58; sh /bigdata/cron.sh&
* * * * * sleep 59; sh /bigdata/cron.sh&
  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值