一、制作服务启动脚本
start.sh
#!/bin/bash
echo "restart web-server,please wait for a moment."
app_name=/home/app/demo.jar
usage(){
echo "usage: start.sh [start|stop|restart|status]"
exit 1
}
is_exist(){
pid=`ps -ef|grep $app_name|grep -v grep|awk '{print $2}'`
echo "pid=${pid}"
if [ -z "${pid}" ]
then
return 0
else
return 1
fi
}
status(){
is_exist
if [ $? -eq 1 ]
then
echo "${app_name} is already running. pid=${pid}"
else
echo "${app_name} is not running"
fi
}
start(){
is_exist
if [ $? -eq 1 ]
then
echo "${app_name} is already running. pid=${pid}"
else
echo "start ${app_name}..."
/home/java/bin/java -Xmx512M -Xms512M -Xmn300M -XX:MaxMetaspaceSize=200M -XX:MetaspaceSize=200M -XX:+UseConcMarkSweepGC -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -XX:+ExplicitGCInvokesConcurrentAndUnloadsClasses -XX:+CMSClassUnloadingEnabled -XX:+ParallelRefProcEnabled -XX:+CMSScavengeBeforeRemark -Xdebug -Xrunjdwp:transport=dt_socket,address=5044,suspend=n,server=y -Djava.security.egd=file:/dev/./urandom -Dspring.profiles.active=dev -jar ${app_name} > /dev/null 2>&1 &
fi
}
stop(){
is_exist
if [ $? -eq 1 ]; then
kill -9 $pid
else
echo "${app_name} is not running."
fi
}
restart(){
stop
sleep 5
start
}
case "$1" in
"start")
start
;;
"stop")
stop
;;
"restart")
restart
;;
"status")
status
;;
*)
usage
;;
esac
用法:
启动服务:./start.sh start
关闭服务:./start.sh stop
重启服务:./start.sh restart
检查服务状态:./start.sh status
二、服务自检脚本
serverCheck.sh
1.该服务会每隔10分钟检查服务状态,发现服务done机,会自动调用启动脚本 ./start.sh start启动服务
2.initserver函数的入参为springboot jar名称
#!/bin/bash
initserver(){
local app_name=$1
local pid=`ps -ef|grep $app_name|grep -v grep|awk '{print $2}'`
if [ -z "${pid}" ]
then
echo `date +%Y-%m-%d` `date +%H:%M:%S` "start server ${app_name} ..." >> shell.log
/home/app/${app_name}/start.sh start
else
echo `date +%Y-%m-%d` `date +%H:%M:%S` "${app_name}:${pid} has been started." >> shell.log
fi
}
while true
do
initserver demo
initserver demo
sleep 600
done
三、添加linux服务器,启动初始化自检脚本
- cd /etc/init.d/ 目录新建webautostart启动脚本文件
vi /etc/init.d/webautostart
webautostart脚本内容
#!/bin/bash
# chkconfig: 3 88 88
/home/app/serverCheck.sh > /dev/null 2>/dev/null
设置执行权限
chmod +x /etc/init.d/webautostart
2.执行命令添加服务到系统自启动列表,并检查是否成功
1)添加服务
chkconfig --add webautostart
2)检查服务是否添加成功
chkconfig --list
image.png
四、重启linux服务器检查服务是否自启动
reboot
网友评论