linux自启动tomcat

第一种方式

1.修改脚本文件rc.local:vim /etc/rc.d/rc.local

  这个脚本是使用者自定的开机启动程序,可以在里面添加想在系统启动之后执行的脚本或者脚本执行命令

2.添加如下内容:

  export JAVA_HOME=/usr/local/jdk1.8.0_171

  /usr/local/apache-tomcat-7.0.88/bin/startup.sh start

3.esc 退出编辑,:wq 保存修改

4.将rc.local修改为可执行

  chmod 777 /etc/rc.d/rc.local

 

第二种方式:

  1. vi /etc/init.d/tomcat  
#!/bin/sh  
# chkconfig: 345 99 10  
# description: Auto-starts tomcat  
# /etc/init.d/tomcatd  
# Tomcat auto-start  
# Source function library.  
#. /etc/init.d/functions  
# source networking configuration.  
#. /etc/sysconfig/network  
RETVAL=0  
export JAVA_HOME=/usr/local/jdk1.8.0_171 

export JRE_HOME=/usr/local/jdk1.8.0_171/jre
export CATALINA_HOME=/usr/local/apache-tomcat-7.0.88
export CATALINA_BASE=/usr/local/apache-tomcat-7.0.88
start()  
{  
        if [ -f $CATALINA_HOME/bin/startup.sh ];  
          then  
            echo $"Starting Tomcat"  
                $CATALINA_HOME/bin/startup.sh  
            RETVAL=$?  
            echo " OK"  
            return $RETVAL  
        fi  
}  
stop()  
{  
        if [ -f $CATALINA_HOME/bin/shutdown.sh ];  
          then  
            echo $"Stopping Tomcat"  
                $CATALINA_HOME/bin/shutdown.sh  
            RETVAL=$?  
            sleep 1  
            ps -fwwu root | grep tomcat|grep -v grep | grep -v PID | awk '{print $2}'|xargs kill -9  
            echo " OK"  
            # [ $RETVAL -eq 0 ] && rm -f /var/lock/...  
            return $RETVAL  
        fi  
}  
  
case "$1" in  
 start)   
        start  
        ;;  
 stop)    
        stop  
        ;;  
                                                  
 restart)  
         echo $"Restaring Tomcat"  
         $0 stop  
         sleep 1  
         $0 start  
         ;;  
 *)  
        echo $"Usage: $0 {start|stop|restart}"  
        exit 1  
        ;;  
esac  
exit $RETVAL  

设置权限:

chmod 755 /etc/init.d/tomcat

设定开机启动服务
  1. sudo chkconfig tomcat on  

启动,停止tomcat

  1. service tomcat start   #或者 /etc/init.d/tomcat start  
  2. service tomcat stop   #或者 /etc/init.d/tomcat stop  
posted on 2018-06-27 22:53  james-roger  阅读(265)  评论(0编辑  收藏  举报