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
下面就是设置定时任务,如没有则用以下命令进行安装和启动:
安装:
扫描二维码关注公众号,回复:
10881873 查看本文章
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&