手写tomcat开机自启脚本

chkconfig命令使用

自启动目录脚本应该放在/etc/init.d/

[root@localhost ~]$ chkconfig --list                 # 列出所有被chkconfig管理的服务
[root@localhost ~]$ chkconfig --add httpd            # 添加指定的服务,让chkconfig指令管理它
[root@localhost ~]$ chkconfig --del httpd            # 删除指定的服务,不再让chkconfig指令管理它
[root@localhost ~]$ chkconfig httpd on               # 设置开机运行服务,需要先执行 --add 才能执行该命令
[root@localhost ~]$ chkconfig httpd off              # 设置开机不运行服务,需要先执行 --add 才能执行该命令
[root@localhost ~]$ chkconfig --level 35 httpd on    # 设置服务在等级3和5时开机运行服务,默认是设置2345等级开机运行服务

创建脚本文件

cd /etc/init.d/
vi ./tomcat

输入如下脚本JRE_HOME、CATALINA_HOME、CATALINA_BASE需要修改.

不知道JRE_HOME可以用命令echo $JAVA_HOME查询。

#!/bin/sh
# chkconfig: 345 99 10
# description: Auto-starts tomcat
# /etc/init.d/tomcat
# Tomcat auto-start
# Source function library.
#. /etc/init.d/functions
# source networking configuration.
#. /etc/sysconfig/network
RETVAL=0
export JRE_HOME=/usr/java/jdk1.7.0_79
export CATALINA_HOME=/usr/local/tomcat/apache-tomcat-7.0.54
export CATALINA_BASE=/usr/local/tomcat/apache-tomcat-7.0.54
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 tomcat | grep apache-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 +x /etc/init.d/tomcat

设置开机自动执行

sudo chkconfig tomcat on
点赞

Leave a Reply

Your email address will not be published. Required fields are marked *