1.服务名和jar包名都为application
/etc/init.d application
application服务内容:
###############################
#!/bin/bash
# chkconfig: 2345 10 90
#服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。
# description: Start and Stop application
. /etc/init.d/functions
SERVICE_NAME="application"
RETVAL=0
PID=-1
PIDFILE=/var/run/${SERVICE_NAME}.PID
start() {
# 首先检查PID文件是否已存在
if [ -f ${PIDFILE} ]; then
echo "PID file ${PIDFILE} already exists, please stop the service !"
exit
fi
echo "Starting service ${SERVICE_NAME} ..."
# >/dev/null 2>&1 表示不输出stdout和stderr
# 最后一个 & 表示整个命令在后台执行
cd "/home/topideal/apps/tms/application"
java -Djava.security.egd=file:/dev/./urandom -jar application.jar --spring.cloud.config.profile=dev --spring.cloud.config.label=test >/dev/null 2>&1 &
PID=$! # 获取本shell启动的最后一个后台程序的进程号(PID)
if [ -z ${PID} ]; then # 检查有没有获取到pid
echo "Failed to get the process id, exit!"
exit
else
echo "Starting successfully, whose pid is ${PID}"
fi
touch $PIDFILE
echo ${PID} > ${PIDFILE}
}
stop() {
if [ -f $PIDFILE ]; then # 检查PIDFILE是否存在
PID=`cat ${PIDFILE}`
if [ -z $PID ]; then # 检查PID是否存在于PIDFILE中
echo "PIDFILE $PIDFILE is empty !"
exit
fi
# 检查该进程是否存在
if [ -z "`ps axf | grep $PID | grep -v grep`" ]; then
echo "Process dead but pidfile exists!"
exit
else
kill -9 $PID
echo "Stopping service successfully , whose pid is $PID"
rm -f $PIDFILE
fi
else
echo "File $PIDFILE does NOT exist!"
fi
}
restart() {
stop
start
}
status() {
# 检查pid file是否存在
if [ -f $PIDFILE ]; then
PID=`cat $PIDFILE`
# 检查pid file是否存在pid
if [ -z $PID ] ; then
echo "No effective pid but pidfile exists!"
else
# 检查pid对应的进程是否还存在
if [ -z "`ps axf | grep $PID | grep -v grep`" ]; then
echo "Process dead but pidfile exist"
else
echo "Running"
fi
fi
else
echo "Service not running"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo "Usage: application {start|stop|restart|status}"
;;
esac
###############################
2.授权
chmod 777 application
#开机启动需要额外配置 chkconfig --add application;chkconfig application on/off
3.开启服务
service application start
# systemctl start/restart/stop/status application(!!!注意观察打印日志是否报错:如找不到文件)
4.红色变绿色
chmod +x application.jar
参考博客:https://blog.csdn.net/zks_4826/article/details/80606036
SpringBoot官网上也有安装方式,这里补充一些链接:
https://www.jianshu.com/p/b61d3c913d15
http://www.hicode.club/2018/06/03/centos-start-sh/
https://juejin.im/post/5b8d2533e51d4538b35c327b
https://blog.csdn.net/ly690226302/article/details/79260875