在Linux系统中,开机启动配置文件通常位于/etc/init.d
目录下,并且是一个脚本文件,该脚本可以通过service
命令或systemctl
命令来启动、停止、重启服务。
1、创建一个服务脚本 /etc/init.d/ruoyi.sh
#!/bin/bashCURRENT_PATH=$(pwd)
JAR_NAME="ruoyi-admin"
APP_PORT=9002
APP_ENV=druidinit() {for name in `find . -name *.jar`doif [[ "${name##*.}"x = "jar"x ]]thenJAR_NAME=${name##*/}echo ${JAR_NAME}returnfidone
}usage() {echo "Usage: sh deploy.sh [start|stop|restart|status] port"exit 1
}status(){pid=`ps -ef|grep ${JAR_NAME}|grep -v grep|awk '{print $2}'`if [[ -z "${pid}" ]]; thenecho "${JAR_NAME} is not running"return 1elseecho "${JAR_NAME} is already running. pid=${pid}"return 0fi
}start(){echo "port ${APP_PORT}"statusif [[ $? -eq 1 ]]; thenecho "${JAR_NAME} is starting"nohup java -jar -Xms200m -Xmx200m -XX:+HeapDumpOnOutOfMemoryError ${JAR_NAME} --spring.profiles.active=${APP_ENV} --server.port=${APP_PORT} --shiro.session.expireTime=10080 --shiro.session.validationInterval=120 --ruoyi.addressEnabled=true --spring.datasource.druid.master.url=jdbc:mysql://localhost:3307/ruoyi?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai > run.log 2>&1 &fi
}stop(){
statusif [[ $? -eq "0" ]]; thenecho "${JAR_NAME} is stopping"#kill ==> kill -15#kill -9 强杀#cat pid | xargs killkill -15 ${pid}#等待一段时间sleep 8#强杀pid=`ps -ef|grep ${JAR_NAME}|grep -v grep|awk '{print $2}'`if [[ -z "${pid}" ]]; thenecho "${JAR_NAME} is closing"elsekill -9 ${pid}fifi
}restart(){stopsleep 3start
}initif [[ -z "$2" ]]; thenecho "no port param!"
elseAPP_PORT=$2
ficase "$1" in"start")start;;"stop")stop;;"status")status;;"restart")restart;;*)usage;;
esac
2、给脚本赋权限
sudo chmod +x /etc/init.d/ruoyi.sh
3、使用systemctl
配置服务以开机启动
sudo systemctl enable ruoyi.sh