1、添加启动脚本,位置任意
[root@localhost sbin]# cat java.sh
#!/bin/sh
###这里必须把JDK环境加进去 不然后面无法自启动
export JAVA_HOME=/home/jdk-11.0.2
export JRE_HOME=\$JAVA_HOME/
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
cd /home/install/admin
pid=`ps -ef|grep 'model-admin-1.0-SNAPSHOT.jar'|grep -v grep|awk '{print $2}'`
if [ -n "${pid}" ];then
kill -9 ${pid}
echo "Process ITSM killed!"
#java 根据自己jdk路径填写, jar包写绝对路径
nohup java -Dfile.encoding=utf-8 -jar model-admin-1.0-SNAPSHOT.jar --spring.profiles.active=ch >nohup.out 2>&1 &
echo "Restart ITSM Success!"
else
nohup java -Dfile.encoding=utf-8 -jar model-admin-1.0-SNAPSHOT.jar --spring.profiles.active=ch >nohup.out 2>&1 &
echo "Start ITSM Success!"
fi
2、授权脚本文件
chmod 777 java.sh
3、配置service文件
[root@localhost sbin]# cat /usr/lib/systemd/system/java.service
[Unit]
Description=java-service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/sbin/java.sh
User=root
Group=root
[Install]
WantedBy=multi-user.target
4、设置为服务
#重载系统服务
systemctl daemon-reload
#测试 服务是否生效
systemctl start java.service
ps aux|grep java
# 设置服务为开机启动服务
systemctl enable java.service
#开机服务查询
systemctl list-unit-files | grep enable | grep java