SpringBoot服务管理shell脚本
2018-06-13 本文已影响0人
sawyerlsy
之前使用jar包发布springboot服务时,每次启动都要敲一串命令,每次停止服务的时候还要找到这个服务kill掉,非常的麻烦。为了提高管理的效率,专门查阅了Linux的一些资料,写了一个shell service.sh
脚本,效果还不错。
#!/bin/bash
OUTPUT_FILE="console.log"
PID_FILE="service.pid"
SERVICE_OPTS="" #-cp . -Xmx128m -Xms128m
SERVICE_JAR="demo.jar"
SERVICE_PORT="10000"
SERVICE_IP="192.168.1.232"
# setting jmx
if [ "$1" = "jmx" ]; then
SERVICE_OPTS="${SERVICE_OPTS} -Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=${SERVICE_PORT} \
-Djava.rmi.server.hostname=${SERVICE_IP} \
-Dcom.sun.managent.jmxremote.authenticate=true \
-Dcom.sun.management.jmxremote.ssl=false "
shift
fi
# start the service
if [ "$1" = "start" ]; then
# According to pid judge whether the program is still running
if [ -n "${PID_FILE}" ] && [ -f ${PID_FILE} ]; then
if [ -s ${PID_FILE} ] && [ -r ${PID_FILE} ]; then
servicePid=`cat ${PID_FILE}`
ps -p ${servicePid} &> /dev/null
if [ $? -eq 0 ]; then
echo "Service appears to still be running with PID ${servicePid}. Start aborted."
echo "If the following process is not a service process, remove the PID file and try again:"
ps -f -p ${servicePid}
exit 1
else
cat /dev/null > ${PID_FILE}
if [ $? -ne 0 ]; then
echo "Unable to clear stale PID file. Start aborted."
fi
fi
fi
fi
# start the program and write pid to the file
if [ -n "${SERVICE_JAR}" ] && [ -f ${SERVICE_JAR} ]; then
nohup java ${SERVICE_OPTS} -jar ${SERVICE_JAR} >> ${OUTPUT_FILE} 2>&1 &
echo $! > ${PID_FILE}
if [ -s ${PID_FILE} ]; then
echo "Service has been started."
exit 0
else
echo "Sorry,service start failed."
exit 1
fi
else
echo "The \$SERVICE_JAR is not set or The jar file does not exist.Start aborted."
exit 1
fi
# stop the service
elif [ "$1" = "stop" ]; then
#According to pid judge whether the program is running
if [ -n "${PID_FILE}" ]; then
if [ -f ${PID_FILE} ]; then
if [ -s ${PID_FILE} ] && [ -r ${PID_FILE} ]; then
servicePid=`cat ${PID_FILE}`
ps -p ${servicePid} &> /dev/null
if [ $? -eq 0 ]; then
kill -9 ${servicePid} &> /dev/null
if [ $? -ne 0 ]; then
echo "Kill process failed. Stop aborted."
exit 1
fi
echo "The Service process has stopped."
# remove pid file
rm -f ${PID_FILE} &> /dev/null
if [ $? -ne 0 ]; then
echo "The ${PID_FILE} removal failed."
exit 1
else
echo "The ${PID_FILE} has been removed."
exit 0
fi
else
echo "PID file found but no matching process was found. Stop aborted."
exit 1
fi
else
echo "PID file is empty or can't read the file.Stop aborted."
exit 1
fi
else
echo "\$SERVICE_PID was set but the specified file does not exist. Is service running? Stop aborted."
exit 1
fi
else
echo "\$SERVICE_PID is not set.Stop aborted."
exit 1
fi
else
echo "Usage: service.sh ( commands ... )"
echo "commands:"
echo " jmx start Start Service under jmx control"
echo " start Start Service"
echo " stop Stop Service"
exit 1
fi
根据个人需要填写service.sh
脚本头部的参数信息,然后直接运行即可,如下:
- 启动服务:./service.sh start
- 停止服务: ./service.sh stop
- 开启jmx监控:./service.sh jmx start
如果需要开启jmx远程监控,需要修改jmxremote.access
和jmxremote.password
,前者用于配置访问权限,后者用于设置密码。这两个文件的默认位置在处于jre的 /lib/management/目录下。如果需要更改位置,可以在service.sh
增加参数:
if [ "$1" = "jmx" ]; then
SERVICE_OPTS="${SERVICE_OPTS} -Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=${SERVICE_PORT} \
-Djava.rmi.server.hostname=${SERVICE_IP} \
-Dcom.sun.managent.jmxremote.authenticate=true \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.acccess.file=/opt/jmxremote.access \
-Dcom.sun.management.jmxremote.password.file=/opt/jmxremote.password"
shift
fi
需要注意的是,这两个文件的权限需要进行调整:
chmod 600 jmxremote.access
chmod 600 jmxremote.password