1.编写脚本ServiceStart.sh【该脚本对存放对目录没有要求,比如我是存在/opt目录下的】
执行命令: vim ServiceStart.sh
# !/bin/bash
# program
# test java open
export JAVA_HOME=/opt/java/jdk1.8.0_231
export JRE=${JAVA_HOME}/jre
export CLASSPATH=$JAVA_HOME/lib:$JRE/lib:.
export PATH=$PATH:$JAVA_HOME/bin/:$JRE/bin
nohup java -jar /opt/project/aa.jar >/opt/project/log/aa.log &
2.将脚本添加到/etc/rc.local【若没有改文件,则手动创建即可】
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
bash /opt/ServiceStart.sh
exit 0
3.修改/etc/rc.local权限
chmod +x /etc/rc.local
4.重新启动【重启服务之前,确保该服务没有人在使用】
reboot
5.重启后查看服务是否正常启动
比如:
1)查看Jar运行的信息
ps -ef|grep /opt/project/aa.jar
2)查看端口状态
netstat -nltp|grep port
说明:
我的系统是ubunt18,此文档对centos系统也同样适用。