当前位置: > CentOS入门 > 系统配置 >

在Centos7上将tomcat加入service服务

时间:2017-07-09 13:32来源:blog.51cto.com 作者:123654zf 的BLOG 举报 点击:

tomcat版本 apache-tomcat-7.0.78

#! /bin/bash
#
# Startup script for the tomcat
# chkconfig: 2345 90 60
 
# source function library
/etc/profile.d/java.sh
/etc/profile.d/tomcat.sh
/etc/rc.d/init.d/functions
 
# tomcat home path
tomcat=/usr/local/apache-tomcat-7.0.78
startup=$tomcat/bin/startup.sh
shutdown=$tomcat/bin/shutdown.sh
 
start() {
    echo -n $"Starting Tomcat service:"
    sh $startup
    echo $?
}
 
stop() {
   echo -n $"Stopping Tomcat service: "
      sh $shutdown
   echo $?
}
 
restart() {
   stop
      start
}
 
status() {
    ps -aef | grep /usr/java/latest/bin/java grep /usr/local/tomcat/ grep -v grep
}
 
case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
status)
    status
    ;;
restart)
    restart
    ;;
*)
    echo $"Usage: $0 {start|stop|restart|status}"
    exit 1
esac
 
exit 0

将其拷入/etc/rc.d/init.d

cd /etc/rc.d/init.d
cp ~/shell/tomcat ./

授权

 
chmod 755 tomcat

添加tomcat服务,并设置开机自启动

chkconfig --add tomcat
chkconfig tomcat on

即可使用如下命令

 
service tomcat {start|stop|restart|status}

踩得坑如下因未导入java环境变量导致启动tomcat后,java环境变量路径显示不全

 
root 19551 1 10 12:14 ? 00:00:02 //bin/java -Djava.util.logging.config.file=/usr/local/apache-tomcat-7.0.78/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.endorsed.dirs=/usr/local/apache-tomcat-7.0.78/endorsed -classpath/usr/local/apache-tomcat-7.0.78/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.78/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/apache-tomcat-7.0.78 -Dcatalina.home=/usr/local/apache-tomcat-7.0.78 -Djava.io.tmpdir=/usr/local/apache-tomcat-7.0.78/temp org.apache.catalina.startup.Bootstrap start

正确的如下

 
root 19672 1 42 12:16 pts/0 00:00:02 /usr/java/latest/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.endorsed.dirs=/usr/local/tomcat/endorsed -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrapstart

解决方法在tomcat脚本中加入如下代码,以导入java环境变量

/etc/profile.d/java.sh
/etc/profile.d/tomcat.sh

------分隔线----------------------------
发表评论
为了和诣的生活,我关闭了评论页面,请大家到QQ群里交流吧:348944156,也欢迎关注本站微信公众号:centoscn