Linux下部署springboot项目jar包开机自启
1、在/etc/init.d/目录下创建shell启动脚本auto_jq.sh(脚本文件名称可自行命名)
cd /etc/init.d/
touch auto_jq.sh
vim auto_jq.sh
写入如下内容
#!/bin/sh
# chkconfig: 2345 85 15
# description:auto_run
#程序名
RUN_NAME="server-1.5_bg.jar"
#jar包位置
JAVA_OPTS=/home/server-1.5_bg.jar
JAVA_YML=/home/application.yml
LOG_OPTS=/home/logs.log
#开始方法
start() {
source /etc/profile; nohup java -jar $JAVA_OPTS --spring.config.location=$JAVA_YML >$LOG_OPTS 2>&1 &
echo "$RUN_NAME started success."
}
#结束方法
stop() {
echo "stopping $RUN_NAME ..."
kill -9 `ps -ef|grep $JAVA_OPTS|grep -v grep|grep -v stop|awk '{print $2}'`
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Userage: $0 {start|stop|restart}"
exit 1
esac
2、设置执行权限
chmod 777 /etc/init.d/auto_jq.sh
chmod 777 /home/server-1.5_bg.jar
3、添加到chkconfig作为系统服务,并设置开机启动:
chkconfig --add autojar.sh (添加为系统服务)
chkconfig autojar.sh on (开机自启动)
service autojar.sh start(启动服务)
reboot (重启服务器)
netstat -ntlp | grep 8080 (查看端口)
ps aux|grep java(查看服务)
配置Tomcat为系统服务并开机自启动
1、新建服务脚本
vim /etc/init.d/tomcat (文件名可自行命名)
填入如下脚本内容:注意更换自己的tomcat文件路径
#!/bin/bash
# description: Tomcat7 Start Stop Restart
# processname: tomcat7
# chkconfig: 234 20 80
CATALINA_HOME=/usr/local/tomcat/apache-tomcat-7.0.77
case $1 in
start)
sh $CATALINA_HOME/bin/startup.sh
;;
stop)
sh $CATALINA_HOME/bin/shutdown.sh
;;
restart)
sh $CATALINA_HOME/bin/shutdown.sh
sh $CATALINA_HOME/bin/startup.sh
;;
*)
echo 'please use : tomcat {start | stop | restart}'
;;
esac
exit 0
执行脚本,启动、停止 和 重启服务。
启动:service tomcat start
停止:service tomcat stop
重启:service tomcat restart
2、Tomcat 配置开启自启动
向chkconfig添加 tomcat 服务的管理
[root@localhost ~]# chkconfig --add tomcat
设置tomcat服务自启动
[root@localhost ~]# chkconfig tomcat on
查看tomcat的启动状态
[root@localhost ~]# chkconfig --list | grep tomcat
状态如下:
[root@localhost ~]# chkconfig –list | grep tomcat
tomcat 0:off 1:off 2:on 3:on 4:on 5:on 6:off
关闭tomcat服务自启动:chkconfig tomcat off
删除tomcat服务在chkconfig上的管理:chkconfig –del tomcat
若启动时报 Neither the JAVA_HOME nor the JRE_HOME environment variable is defined 错误
配置完成 启动服务时出现如下错误日志:
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program
字面理解可看出是 JDK 问题,首先先查看JAVA_HOME路径
vim /etc/profile
编辑文件 tomcat bin目录下的 catalina.sh文件
在第二行中插入:export JAVA_HOME="JAVA_HOME路径"