爱玩科技网
您的当前位置:首页linux开机自启tomcat项目,Linux下配置tomcat、springboot项目jar包开机自启

linux开机自启tomcat项目,Linux下配置tomcat、springboot项目jar包开机自启

来源:爱玩科技网

Linux下配置tomcat、springboot项目jar包开机自启

Linux下配置tomcat、springboot项目jar包开机自启

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 服务的管理

[[email protected] ~]# chkconfig --add tomcat

设置tomcat服务自启动

[[email protected] ~]# chkconfig tomcat on

查看tomcat的启动状态

[[email protected] ~]# chkconfig --list | grep tomcat

状态如下:

[[email protected] ~]# 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

在第二行中插入:export JAVA_HOME="JAVA_HOME路径"

Linux下配置tomcat、springboot项目jar包开机自启相关教程

因篇幅问题不能全部显示,请点此查看更多更全内容