1、编辑脚本名称为 /home/test.sh的
#!/bin/sh
# jar包名称
APP_NAME=/home/xxx-1.8.2.jar
# 日志输出名称
APP_LOG=/home/sentinel.log
#运行脚本提示信息
tips(){
echo "-------------------------------------"
echo ""
echo "项目地址: ${APP_NAME}"
echo ""
echo "你可以使用如下参数进行操作"
echo "-status -查看当前项目运行状态"
echo "-start -启动当前项目"
echo "-stop -停止当前项目"
echo "-restart -重启当前项目"
echo ""
echo "-------------------------------------"
}
#
usage(){
echo "Usage: sh demo.sh [start|stop|restart|status]"
exit 1
}
#检查程序是否在运行
is_exist() {
pid=`ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}' `
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
#启动方法
start() {
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid} ."
else
nohup java -Dserver.port=8086 -Dproject.name=sentinel-dashboard -jar ${APP_NAME} >${APP_LOG} 2>&1 &
fi
}
#停止方法
stop() {
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
else
echo "${APP_NAME} is not running"
fi
}
#输出运行状态
status() {
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is not running."
fi
}
#重启
restart() {
stop
start
}
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
2、给/etc/rc.d/rc.local/开机启动项增加权限
命令:chmod +x /etc/rc.d/rc.local
3、编辑:vi /etc/rc.d/rc.local 增加脚本内容
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
export JAVA_HOME=/home/java/jdk1.8.0_111
export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
export PATH=$PATH:${JAVA_HOME}/bin
touch /var/lock/subsys/local
sh /home/test.sh start
注意:
注意:
注意:
重要的事情说三遍:如果想修改jar包中内置的tomcat的内存修改命令:新增 -Xms256m -Xmx256m 具体如下
nohup java -Xms256m -Xmx256m -Dserver.port=${APP_PORT} -Dproject.name=${APP_NAME_JAR} -jar ${APP_NAME} >${APP_LOG} 2>&1 &