一、编写启动脚本
#!/bin/bash SCRIPTPATH=`dirname $0` PWDPATH=`cd $SCRIPTPATH && pwd` # 配置jar home路径,按照实际配置修改 #JRE_HOME=/data/jdk1.8.0/jre # jvm 允许参数 JVM_OPTION=" -Xms512m -Xmx2g" # 项目配置 jar路径 SPRING_CLASSPATH=$PWDPATH/lib/*
#项目配置文件 SPRING_CONFIG=file:$PWDPATH/conf/ SPRING_CONFIG=$PWDPATH/conf/bootstrap.properties SPRING_LOGGING=$PWDPATH/conf/logback.xml SIGAR=$PWDPATH/sigar # 删除服务后启动 PROCESS=$(ps -ef | grep $PWDPATH/lib | grep -v grep | awk '{print $2}') echo "$PROCESS" if [ -n "$PROCESS" ]; then echo "PROCESS no IS not NULL" kill -9 $PROCESS fi echo "------------ begin starting $PWDPATH -------------" OPTION="$JVM_OPTION -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$PWDPATH/logs" SPRING_PARAMS="--spring.config.location=$SPRING_CONFIG --logging.config=$SPRING_LOGGING" nohup java -server $OPTION -Djavax.servlet.request.encoding=UTF-8 -Dfile.encoding=UTF-8 -Duser.language=zh_CN -Dsun.jnu.encoding=UTF-8 -Djava.library.path=$SIGAR -Djava.security.egd=file:/dev/./urandom -jar $SPRING_CLASSPATH $SPRING_PARAMS > /dev/null 2>&1 & new_agent_pid=$! #java -server $OPTION -Djava.security.egd=file:/dev/./urandom -jar $SPRING_CLASSPATH $SPRING_PARAMS echo "------------ end start $new_agent_pid $PWDPATH -------------"
这个-Djavax.servlet.request.encoding=UTF-8 -Dfile.encoding=UTF-8 -Duser.language=zh_CN -Dsun.jnu.encoding=UTF-8 命令主要是为了解决日志输出中文乱码
二、编写关闭脚本
#!/bin/bash SCRIPTPATH=`dirname $0` PWDPATH=`cd $SCRIPTPATH && pwd` #rm -fr native PROCESS=$(ps -ef | grep $PWDPATH/lib | grep -v grep | awk '{print $2}') echo "$PROCESS" if [ -n "$PROCESS" ]; then echo "PROCESS no IS not NULL" kill -9 $PROCESS fi
这些脚本存放在项目的根路径下面
三、编写开机启动脚本
#!/bin/bash #chkconfig:2345 80 90 #decription: FaceDeviceCenter start #SCRIPTPATH=`dirname $0` #PWDPATH=`cd $SCRIPTPATH && pwd`
#项目路径 PWDPATH=/mnt/server/FaceDeviceCenter-8083 # 配置jar home路径,按照实际配置修改 #JRE_HOME=/data/jdk1.8.0/jre # jvm 允许参数 JVM_OPTION=" -Xms512m -Xmx2g" # 项目配置 SPRING_CLASSPATH=$PWDPATH/lib/* SPRING_CONFIG=file:$PWDPATH/conf/ SPRING_CONFIG=$PWDPATH/conf/bootstrap.properties SPRING_LOGGING=$PWDPATH/conf/logback.xml SIGAR=$PWDPATH/sigar # 删除服务后启动 PROCESS=$(ps -ef | grep $PWDPATH/lib | grep -v grep | awk '{print $2}') echo "$PROCESS" if [ -n "$PROCESS" ]; then echo "PROCESS no IS not NULL" kill -9 $PROCESS fi echo "------------ begin starting $PWDPATH -------------" OPTION="$JVM_OPTION -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$PWDPATH/logs" SPRING_PARAMS="--spring.config.location=$SPRING_CONFIG --logging.config=$SPRING_LOGGING" nohup java -server $OPTION -Djavax.servlet.request.encoding=UTF-8 -Dfile.encoding=UTF-8 -Duser.language=zh_CN -Dsun.jnu.encoding=UTF-8 -Djava.library.path=$SIGAR -Djava.security.egd=file:/dev/./urandom -jar $SPRING_CLASSPATH $SPRING_PARAMS > /dev/null 2>&1 & new_agent_pid=$! #java -server $OPTION -Djava.security.egd=file:/dev/./urandom -jar $SPRING_CLASSPATH $SPRING_PARAMS echo "------------ end start $new_agent_pid $PWDPATH -------------"
-
注意脚本要添加这两行代码,否则会启动不了
#chkconfig:2345 80 90 #decription:autostart - 将脚本移动到
/etc/rc.d/init.d
目录下,或者在这个目录下创建一个xxxx.sh的脚本文件,一般我喜欢把项目名称作为脚本文件名,并把上面的脚本内容写进去,比如我脚本名tomcat.sh为例
mv /xxxx/tomcat.sh /etc/rc.d/init.d
- 增加脚本的可执行权限
chmod +x /etc/rc.d/init.d/tomcat.sh
- 添加脚本到开机自动启动项目中
cd /etc/rc.d/init.d chkconfig --add tomcat.sh chkconfig tomcat.sh on